procedure TForm1.Button1Click(Sender: TObject); begin if listbox1.items.IndexOf(edit1.text)=-1 then listbox1.Items.Add(edit1.text) else if application.MessageBox('发现重复,是否要继续增加?','提示',Mb_yesno+mb_iconquestion)=idyes then begin listbox1.Items.Add(edit1.text); end; end;procedure TForm1.FormCreate(Sender: TObject); begin listbox1.Items.LoadFromFile('c:\test.txt'); end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin listbox1.Items.SaveToFile('c:\test.txt'); end;
form1中的控件: button1,edit1,memo1,opendialog1 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; OpenDialog1: TOpenDialog; Memo1: TMemo; Edit1: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); var i:integer; // counter substr,s:string; //substr:所要检查的字符串 f:textfile; //文件 begin i:=0; substr:=edit1.text; //edit1里输入要查的字符串 if opendialog1.Execute then //打开文件 begin assignfile(f,opendialog1.FileName); //定位文件 reset(f); //打开文件 while not eof(f) do //当文件没结束时 begin i:=i+1; //计数器加1 readln(f,s); //读一行并光标换至下一行 if pos(substr,s)<>0 then //当这行有要查的字符串 showmessage('string:'''+substr+'''is in line'+inttostr(i)); //显示:(e.g.)string:'abc'is in line 13; memo1.Lines.Add(inttostr(i)+':'+s); //memo1里将显示文本文件内容,并在前边加上行数 end; end; end;end.
string:'abc'is in line 13; 这一行被自动换行的,是注释。 另外可以多看看delphi的help吗,这样50分就省下来了。
我觉得应该是循环(读取每一行)+pos这样子
如果搜索 “你好” 两个字,而文本中 在“你” 字 后面就换行了,是否应该全部读出来再用POS
如果字符串比较长,建议用KMP字符串匹配算法代替“传统”的循环。
前面的几位说的都是在memo,或者是list...... 文件应该象下面 procedure TForm1.Button1Click(Sender: TObject); var i:integer; substr,s:string; f:textfile; begin i:=0; substr:='a'; if opendialog1.Execute then //打开文件 begin assignfile(f,opendialog1.FileName); //定位文件 reset(f); //打开文件 while not eof(f) do //当文件没结束时 begin i:=i+1; readln(f,s); if pos(substr,s)<>0 then showmessage('string:'''+substr+'''is in line'+inttostr(i)); string:'abc'is in line 13; memo1.Lines.Add(inttostr(i)+':'+s); end; end; end;
begin
if listbox1.items.IndexOf(edit1.text)=-1 then
listbox1.Items.Add(edit1.text)
else
if application.MessageBox('发现重复,是否要继续增加?','提示',Mb_yesno+mb_iconquestion)=idyes then
begin
listbox1.Items.Add(edit1.text);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
listbox1.Items.LoadFromFile('c:\test.txt');
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
listbox1.Items.SaveToFile('c:\test.txt');
end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
Memo1: TMemo;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
i:integer; // counter
substr,s:string; //substr:所要检查的字符串
f:textfile; //文件
begin
i:=0;
substr:=edit1.text; //edit1里输入要查的字符串
if opendialog1.Execute then //打开文件
begin
assignfile(f,opendialog1.FileName); //定位文件
reset(f); //打开文件
while not eof(f) do //当文件没结束时
begin
i:=i+1; //计数器加1
readln(f,s); //读一行并光标换至下一行
if pos(substr,s)<>0 then //当这行有要查的字符串
showmessage('string:'''+substr+'''is in line'+inttostr(i)); //显示:(e.g.)string:'abc'is in line 13;
memo1.Lines.Add(inttostr(i)+':'+s); //memo1里将显示文本文件内容,并在前边加上行数
end;
end;
end;end.
另外可以多看看delphi的help吗,这样50分就省下来了。
文件应该象下面
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
substr,s:string;
f:textfile;
begin
i:=0;
substr:='a';
if opendialog1.Execute then //打开文件
begin
assignfile(f,opendialog1.FileName); //定位文件
reset(f); //打开文件
while not eof(f) do //当文件没结束时
begin
i:=i+1;
readln(f,s);
if pos(substr,s)<>0 then
showmessage('string:'''+substr+'''is in line'+inttostr(i));
string:'abc'is in line 13;
memo1.Lines.Add(inttostr(i)+':'+s);
end;
end;
end;