Board index » delphi » URGENT HELP WITH STRING MANIPULATION REQUIRED!!!!

URGENT HELP WITH STRING MANIPULATION REQUIRED!!!!

Hi,

I have a very large text file (2-4 megs) which contains simply one large
string!! ... I need to search for an occurance of a sub-string, and place
a carraige return in front of this sub-string. The furthest I have got is
shown below. I would really appreciate any help on this one as I am
getting no where fast!

Thanks,

Allen.
(please reply to "t...@cargo.iol.ie")

Say we place the carraige return in front of string 'BJH' then example as
follows:

Example input file =

XXXBJHXBJHXXBJHXXXBJHkkBJOBJHXX

Example putput file =

XXX
BJHX
BJHXX
BJHXXX
BJHkkBJO
BJHXX

This is as far as I have got!
========================================================

Procedure BreakString;
var
s:string;
FileName:string;
FileStream : TFileStream;
f:textfile;
x,i:integer;
begin
FileName := Edit1.text;
FileStream := TFileStream.Create(FileName,fmOpenRead);
FileStream.Seek(0,0);
SetLength(S,FileStream.Size);
FileStream.Read(S[1],FileStream.Size);
memo1.lines.add(IntToStr(Length(s)));
{For test purposes the input file simply containsthe
string'XXXBJHXBJHXXBJHXXXBJHkkBJOBJHXX'
 - this input file should result in an output file containing five
lines!}

{For i := 1 to Length(S) Do If Copy(S,i,3) = 'BJH' then
insert(#13+#10,S,i-1);}

  AssignFile(F, 'result.txt');
  Rewrite(F);
  Writeln(F, S);  {Write in the ammended string with carraige returns!}
  CloseFile(F);
Beep;
End;

--
(To reply remove the NO from my address!)

 

Re:URGENT HELP WITH STRING MANIPULATION REQUIRED!!!!


In article <MPG.f52a6a39d857221989...@news.indigo.ie>, tec...@cargo.iol.ie

Quote
(Allen) writes:
>I have a very large text file (2-4 megs) which contains simply one large
>string!! ... I need to search for an occurance of a sub-string, and place
>a carraige return in front of this sub-string. The furthest I have got is
>shown below. I would really appreciate any help on this one as I am
>getting no where fast!

Quick and Dirty . . .

Ident := string to find
Open memory stream
Open FileStream
Seek beginning
i := 1
while i <= StreamSize
 read 1 byte from filestream
 if byte = Ident[0] then  {if 1}
   TempStr := Byte
    read 1 byte from filesteam
    if byte = ident[2] then {if 2}
      TempStr := TempStr + byte
      read i byte from string
      if byte = ident[3] then  {if 3}
        TempStr := TempStr + byte
        write CRLF + TempStr to memory stream
      else {3}
        write TempStream to memory stream;
       {end if 3}
    else {2}
        write TempStream to memory stream;
        {end if 2}
  else [1]
       write TempStream to memory stream;
  {end if 1}
inc(i)
{end for}
write memory stream to output file.

This could be polished but if you've only got a few to do it's hardly worth it.

. . . or put it into MS Word and do it with search/replace !!

Alan Lloyd
alangll...@aol.com

Re:URGENT HELP WITH STRING MANIPULATION REQUIRED!!!!


Quote
On Mon, 16 Feb 1998 19:40:58 -0000, tec...@cargo.iol.ie (Allen) wrote:
>Hi,

>I have a very large text file (2-4 megs) which contains simply one large
>string!! ... I need to search for an occurance of a sub-string, and place
>a carraige return in front of this sub-string. The furthest I have got is
>shown below. I would really appreciate any help on this one as I am
>getting no where fast!

>Thanks,

>Allen.
>(please reply to "t...@cargo.iol.ie")

The problem is that the string gets changed when you insert into it,
so your index i is no longer where you think it is...

This small example works:
procedure TForm1.FormCreate(Sender: TObject);
var
   S:    String;
   i:    Integer;
begin
   S := 'XXXBJHXBJHXXBJHXXXBJHkkBJOBJHXX';
   i := 1;
   while i < Length(S)-1 do
      if Copy(S,i,3) = 'BJH' then
         begin
            insert('##', S, i);
            i := i+3;
         end
      else
         inc(i);
   Label1.Caption := S;
end;

Now, given the size of the string, I would try to avoid using *insert*
and better concatenete to another, initially empty, string, token by
token...

Pierre Gemis

Other Threads