如何用delphi实现在一个文本文件中查找文字,并给出相应提示(100分)

  • 主题发起人 主题发起人 wl2001
  • 开始时间 开始时间
W

wl2001

Unregistered / Unconfirmed
GUEST, unregistred user!
如何用delphi实现在一个文本文件中查找文字,并给出相应提示
 
1.先搜索
2,看帮助
我有例程,不过不知找不找得到,如果还不会,我去找找 :)
 
如果文件小的话,你试试把内容读进一个大字符串里,然后用POS判断。
文件大的话,你试试分块读入大字符串里,然后用POS判断。记得分块时要下一段的头部和
上一段的尾部重合要寻找的字符串的长度。
以上意见,纯粹仅供参考。呵呵我也没做过。对不起了。
 
我这里有一段代码。
unit unit2;

interface

uses
richedit, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
findbutton: TButton;
Fedit: TEdit;
OpenDialog1: TOpenDialog;
richedit: TRichEdit;
wword: TCheckBox;
mcase: TCheckBox;
searchup: TCheckBox;
Redit: TEdit;
replacebutton: TButton;
starttop: TCheckBox;
replallbutton: TButton;
Label1: TLabel;
Label2: TLabel;
openbutton: TButton;
Exitbutton: TButton;
procedure FormCreate(Sender: TObject);
procedure openbuttonClick(Sender: TObject);
procedure findbuttonClick(Sender: TObject);
procedure replacebuttonClick(Sender: TObject);
procedure replallbuttonClick(Sender: TObject);
function find(tofind,replacewith: string): string;
procedure ExitbuttonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
replall: boolean;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
{make richedit edit 32+k files. MUST have 'richedit' in USES above}
richedit.perform(EM_EXLIMITTEXT, 0, 8000000);

end;

procedure TForm1.openbuttonClick(Sender: TObject);
begin
if opendialog1.execute
then richedit.lines.loadfromfile( opendialog1.filename);
end;

procedure TForm1.findbuttonClick(Sender: TObject);
var
tofind,replacewith: string;
begin
if Fedit.text <> '' then find(tofind,replacewith);
end;

procedure TForm1.replacebuttonClick(Sender: TObject);
var
tofind,replacewith: string;
begin
if Fedit.text <> '' then
begin
tofind := Fedit.text;
replacewith := Redit.text;
find(tofind,replacewith)
end;
end;

procedure TForm1.replallbuttonClick(Sender: TObject);
var
tofind,replacewith: string;
begin
if Fedit.text <> '' then
begin
tofind := fedit.text;
replacewith := redit.text;
replall := true; {replall = global boolean var}
while replall = true do find(tofind,replacewith);
end;
end;

function TForm1.find(tofind,replacewith: string): string;
var
matchcase,wholeword,up,top: boolean;
FindIn,findin2,uptofind,upfindin,upfindin2: string;
find1s,find2s,find3s,find4s: string;
find1,find2,find3,find4: integer;
flen,start,holder,found,index,i: integer;
begin
{check status of option checkboxes}
if mcase.checked then matchcase := true;
if wword.checked then wholeword := true;
if searchup.checked then up := true;
if starttop.checked then top := true;

if top = true then
begin
richedit.perform(em_setsel,0,0);
starttop.checked := false;
top := false;
end;

{Load Fedit.text as var. If replacing, replace selected text
if it is = to Fedit. Then load document text as string var. The
string vars are case sensitive as strings so lowercase them if
that option is not checked.}
if matchcase = true then
begin
ToFind := Fedit.text;
if lowercase(richedit.seltext) = lowercase(tofind) then
if replacewith <> '' then richedit.seltext := replacewith;
findin := richedit.text;
end
else
begin
ToFind := lowercase(fedit.text);
if replacewith <> '' then replacewith := lowercase(replacewith);
if lowercase(richedit.seltext) = lowercase(tofind) then
if replacewith <> '' then richedit.seltext := replacewith;
findin := lowercase(richedit.text);
end;
flen := length(tofind);

found := 0;
index := -1;
if up = true then
{reverse contents of fedit and richedit. Perform pos() search
for reversed fedit. It's found value is then reversed so that the
visible richedit can use it as an em_selstart value (em_selstart
because 'selstart :=' gets funky after 32k and 64k)}
begin
start := richedit.selstart;
holder := start +1;
for i := 0 to length(tofind)-1 do appendstr(uptofind, copy(tofind,length(tofind)-i,1));
while found = 0 do
begin
if holder < -250 then
begin
showmessage('"'+tofind+'" cannot be found');
replall := false; {replall = global boolean var}
activecontrol := richedit;
exit;
end;
if holder < 250 then i := holder -1
else
begin
i := 250;
end;
{add spaces to ends of searchin string for wholeword search}
findin2 := concat(' ',copy(findin,holder-i,i),' ');{grab 250 chunks to search}
upfindin2 := '';
for i := 0 to length(findin2)-1 do appendstr(upfindin2, copy(findin2,length(findin2)-i,1));
if wholeword = true then
begin
find1s := ' ' + uptofind + ' ';
find2s := #13 + uptofind + ' ';
find3s := #13 + uptofind + #10;
find4s := ' ' + uptofind + #10;
find1 := pos(find1s, upfindin2); {find each type of wholeword}
find2 := pos(find2s, upfindin2);
find3 := pos(find3s, upfindin2);
find4 := pos(find4s, upfindin2);
if find1 = 0 then find1 := 300; {change notfound for next test}
if find2 = 0 then find2 := 300;
if find3 = 0 then find3 := 300;
if find4 = 0 then find4 := 300;
{find the lowest found value (first found) and set 'found' to it}
if (find1 <300) and (find1 <find2) and (find1 <find3) and (find1 <find4)
then found := find1 +1;
if (find2 <300) and (find2 <find1) and (find2 <find3) and (find2 <find4)
then found := find2 +1;
if (find3 <300) and (find3 <find1) and (find3 <find2) and (find3 <find4)
then found := find3 +1;
if (find4 <300) and (find4 <find1) and (find4 <find2) and (find4 <find3)
then found := find4 +1;
end
else found := pos(uptofind, upfindin2); {not wholeword search}
holder := holder - 225; {225 to allow for possible truncation}
index := index +1;
end;
if index > 0 then index := index *225 else index := 0;
holder := start -index -found +3;
ActiveControl := richedit;
richedit.perform(em_setsel, holder-flen, holder);
richedit.perform(em_scrollcaret, 0,0);
end
else {search up is not checked}
begin
start := richedit.selstart;
if lowercase(richedit.seltext) = lowercase(tofind) then
richedit.perform(em_setsel, start +flen, start +flen);
start := richedit.selstart;
holder := start +1;
while found = 0 do
begin
if holder > (richedit.gettextlen)+223 then
begin
showmessage('"'+tofind+'" cannot be found');
replall := false; {replall = global boolean var}
ActiveControl := richedit;
exit;
end;
findin2 := concat(' ',copy(findin,holder,250),' ');
if wholeword = true then
begin
find1s := ' ' + tofind + ' ';
find2s := #10 + tofind + ' ';
find3s := #10 + tofind + #13;
find4s := ' ' + tofind + #13;
find1 := pos(find1s, findin2);
find2 := pos(find2s, findin2);
find3 := pos(find3s, findin2);
find4 := pos(find4s, findin2);
if find1 = 0 then find1 := 300;
if find2 = 0 then find2 := 300;
if find3 = 0 then find3 := 300;
if find4 = 0 then find4 := 300;
if (find1 <300) and (find1 <find2) and (find1 <find3) and (find1 <find4)
then found := find1 +1;
if (find2 <300) and (find2 <find1) and (find2 <find3) and (find2 <find4)
then found := find2 +1;
if (find3 <300) and (find3 <find1) and (find3 <find2) and (find3 <find4)
then found := find3 +1;
if (find4 <300) and (find4 <find1) and (find4 <find2) and (find4 <find3)
then found := find4 +1;
end
else found := pos(tofind, findin2);
holder := holder + 225;
index := index +1;
end;
if index > 0 then index := index *225 else index := 0;
holder := start +index +found-3;
ActiveControl := richedit;
richedit.perform(em_setsel, holder, holder +flen);
richedit.perform(em_scrollcaret, 0,0);
end;
end;

procedure TForm1.ExitbuttonClick(Sender: TObject);
begin
close;
end;

end.
 
多人接受答案了。
 
后退
顶部