有一种算法叫状态机,就是你想要的,搜一下就行了。 在 Delphi算法与结构 这本书上!Function TCountWord.ExtractWords(const S: string): TStringDynArray; type TStates = (ScanNormal,ScanQuoted,ScanPunctuation); const WordDelim = ' !<>[](){},./?;~:-+=*@#$%^&"12345567890'; var State: TStates; Inx: integer; Ch: Char; CurWord: string; procedure AddToList(Const aWord: string); var aLen: integer; begin aLen := Length(Result); SetLength(Result,aLen+1); Result[aLen] := lowerCase(aWord); end; begin SetLength(Result,0); State := ScanNormal; CurWord :=''; //读入串中所有字符 for Inx := 1 to Length(S) do begin //获取下一个字符 Ch := S[Inx]; //对于状分情况处理 case State of ScanNormal: begin if Pos(Ch,WordDelim)<>0 then begin if (CurWord<>'') then begin AddToList(CurWord); CurWord := ''; end; state := ScanPunctuation; end else begin CurWord := CurWord+Ch; end; end; ScanQuoted: begin CurWord := CurWord+Ch; State := ScanNormal; end; ScanPunctuation: begin if Pos(Ch,WordDelim) = 0 then begin CurWord := Ch; State := ScanNormal; end; end; end; end; if CurWord<>'' then AddToList(CurWord); end;
用stringlist,把全部文章load进来
然后find函数参数用关键字找出你想要的文章。
type
TStates = (ScanNormal,ScanQuoted,ScanPunctuation);
const
WordDelim = ' !<>[](){},./?;~:-+=*@#$%^&"12345567890';
var
State: TStates;
Inx: integer;
Ch: Char;
CurWord: string; procedure AddToList(Const aWord: string);
var
aLen: integer;
begin
aLen := Length(Result);
SetLength(Result,aLen+1);
Result[aLen] := lowerCase(aWord);
end;
begin
SetLength(Result,0);
State := ScanNormal;
CurWord :='';
//读入串中所有字符
for Inx := 1 to Length(S) do
begin
//获取下一个字符
Ch := S[Inx];
//对于状分情况处理
case State of
ScanNormal:
begin
if Pos(Ch,WordDelim)<>0 then
begin
if (CurWord<>'') then
begin
AddToList(CurWord);
CurWord := '';
end;
state := ScanPunctuation;
end else
begin
CurWord := CurWord+Ch;
end;
end;
ScanQuoted:
begin
CurWord := CurWord+Ch;
State := ScanNormal;
end;
ScanPunctuation:
begin
if Pos(Ch,WordDelim) = 0 then
begin
CurWord := Ch;
State := ScanNormal;
end;
end; end;
end;
if CurWord<>'' then
AddToList(CurWord);
end;
like SQL语句不行吗?