Mega Code Archive

 
Categories / Delphi / Examples
 

Findtextfiletext

BESIDES using Boyer-Moore code (lots of it elsewhere on Mai Ling [Richards Computer]) here's a very succinct method for finding text IN A TEXT FILE... Load it into a TStringList and then work through that list: function FindText(const Text2Find: String; const FilePath: String): String= ; var txtFile: TStringList; i: Integer; begin if Text2Find =3D '' then Exit; if FilePath =3D '' then Exit; Result :=3D ''; txtFile :=3D TStringList.Create; try if NOT FileExists(FilePath) then Exit; txtFile.LoadFromFile(FilePath); for i :=3D 0 to txtFile.Count - 1 do begin if Pos(Text2Find,txtFile[i]) > 0 then Result :=3D txtFile[i]; end; finally txtFile.Free; end; end; **************************************************** After you have loaded the file into MyStringList, use the Text property TempVar := pos(TextToSearch, MyStringList.Text); Since this method reads the entire file into memory, it will not work for large files. (Anybody know the limit for TString?) At 07:39 AM 2/28/99 +0400, Santosh Pillai wrote: > >One of the ways would be to load the text file into a string list with > >MyStringList.LoadFromFile( FileName ); > >and then looping through the string list > >for I := 0 to MyStringList.Count-1 do > TempVar := Pos( TextToSearch, MyStringList[i] );