Re: Re: How to search for a string in a text-file


Posted by Guido, webmaster on August 18, 2000 at 19:51:14:

In Reply to: Re: How to search for a string in a text-file posted by Jean Claude Servaye on August 16, 2000 at 18:02:08:

: var
: F : TextFile;
: A,B : String;
: begin
: B := 'StringToFind';
: AssignFile(F,'YourTextFile.Txt';
: Reset(F);
: While not EOF(F) do begin
: Readln(F,A);
: If Pos(B,S) > 0 then
: begin
: //Your code here
: Break // if you have only one string to search
: end;
: end;
: CloseFile(F);
: end;
---
Or something like this function, that returns the line number where the string is found, or a negative number if not found:
function FindInFileString(FilePath,
SearchString: string): integer;
var
SL: TStringList;
i: integer;
begin
try
Result := -1;
SL := TStringList.Create;
SL.ReadFromFile(FilePath);
for i := 0 to SL.Count - 1 do
if Pos(SearchString, SL[i]) > 0 then begin
Result := i;
break;
end;
finally
SL.Free;
end;
end;



Related Articles and Replies: