//很久以前的一个列子
//字符串匹配的问题
//你要整理一下,可能会对你有用
unit CZZ;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ComCtrls, Menus;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
P: TEdit;
BitBtn1: TBitBtn;
T: TRichEdit;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
Op: TOpenDialog;
Ne: TBitBtn;
Label3: TLabel;
SM: TEdit;
Label4: TLabel;
procedure BitBtn1Click(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure NeClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
I,J:Integer;
M,N:Integer;
Form1: TForm1;
Function PSPP(TT:String;PP:String):Boolean;
implementation
{$R *.DFM}
Function PSPP(TT:String;PP:String):Boolean;
Begin
i:=1;
M:=Length(PP);
N:=Length(TT);
While (i<=M) And (j<=N) Do
Begin
IF PP=TT[j] Then
Begin
i:=i+1;
j:=j+1;
End
Else
Begin
j:=j-i+2;
i:=1;
End;
End;
IF i-1=M Then
PSPP:=True
Else
PSPP:=False;
End;
procedure TForm1.BitBtn1Click(Sender: TObject);
Var
TT,PP:String;
begin
//查找
SM.Text:='0';
PP:=P.Text;
TT:=T.Text;
J:=1;
T.HideSelection:=False;
IF PSPP(TT,PP) Then
Begin
SM.Text:=IntToStr(StrToInt(SM.Text)+1);
T.SelStart:=J-M-1;
T.SelLength:=M;
Ne.Enabled:=True;
End;
end;
procedure TForm1.N1Click(Sender: TObject);
begin
IF Op.Execute Then
T.Lines.LoadFromFile(Op.FileName);
end;
procedure TForm1.NeClick(Sender: TObject);
Var
TT,PP:String;
JG:Boolean;
begin
//查找下一个
PP:=P.Text;
TT:=T.Text;
JG:=PSPP(TT,PP);
IF JG Then //123456789
Begin
SM.Text:=IntToStr(StrToInt(SM.Text)+1);
T.SelStart:=J-M-1;
T.SelLength:=M;
End
Else
Ne.Enabled:=False;
end;
end.