delphi搜索txt文件的指定内容!

2024-12-17 01:10:55
推荐回答(1个)
回答1:

方便比较笨,不过可以实现。

----------------------------------------------------
test.txt的内容是:

哈哈,haha,123
呵呵,hehe,456

----------------------------------------------------

var
i,IstrIndex:Integer;
SstrList,SextractList,SsubstrList:TStringList;
begin
SstrList :=TStringList.Create;
SextractList :=TStringList.Create;
SsubstrList :=TStringList.Create;
try
SstrList.LoadFromFile('test.txt');
for i:=0 to SstrList.Count -1 do begin
SextractList.Delimiter :=',';
SextractList.DelimitedText :=SstrList[i];
SsubstrList.Add(SextractList[0]);
end;
if Pos(Edit1.Text,SsubstrList.Text) <>0 then begin
IstrIndex :=SsubstrList.IndexOf(Edit1.Text);
SsubstrList.Delimiter :=',';
SsubstrList.DelimitedText :=SstrList[IstrIndex];
Edit2.Text :=SsubstrList[2];
end;
finally
SstrList.Free;
SextractList.Free;
SsubstrList.Free;
end;
end;