delphi搜索txt文件的指定内容!
delphi按知道格式读取txt文件的内容,现有xx.txt文件,比如txt文件中每行的格式为:哈哈,haha,123呵呵,hehe,456我想在edit1中输入“哈哈”...
delphi按知道格式读取txt文件的内容,
现有xx.txt文件,比如txt文件中每行的格式为:
哈哈,haha,123
呵呵,hehe,456
我想在edit1中输入“哈哈”然后edit2中就显示“123”,要怎么写代码 展开
现有xx.txt文件,比如txt文件中每行的格式为:
哈哈,haha,123
呵呵,hehe,456
我想在edit1中输入“哈哈”然后edit2中就显示“123”,要怎么写代码 展开
展开全部
方便比较笨,不过可以实现。
----------------------------------------------------
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;
----------------------------------------------------
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;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询