下面一个是我以前写的类, 它可以使用任意的字符来作为单词的间隔
例如:
I Love You
使用 'o' 来做间隔, 可得到:
I L
ve Y
u
几个单词
//-----------------------------------------
{
TStringTokenizer
prog by xiao.lit
example:
const
Seps : SeperatorSet = [' ','o']; //use space and char 'o' for seperator
var
t:TStringTokenizer;
s:String;
begin
t := TStringTokenizer.Create('I Love you', seps);
while t.HaveMoreWords do begin
s := t.GetNextWord;
ShowMessage(s)
end;
t.Free;
end;
}
unit StringTokenizer;
interface
type
SeperatorSet = set of char;
TStringTokenizer = class
private
Words : String; {String to be tokenized}
Seperators : SeperatorSet; {Seperator between words}
Offset : Integer; {offset of the current word pointer}
public
Constructor Create(Line:String ; Sep : SeperatorSet);
function HaveMoreWords : boolean;
function GetNextWord : string;
end;
implementation
constructor TStringTokenizer.Create(Line : String; Sep : SeperatorSet);
begin
Words := Line;
Seperators := Sep;
offset := 1;
end;
function TStringTokenizer.HaveMoreWords : boolean;
begin
while((Words[offset] in Seperators) and (offset <= Length(Words))) do
inc(Offset);
if Offset <= Length(Words) then HaveMoreWords := true
else HaveMoreWords := false;
end;
function TStringTokenizer.GetNextWord : string;
var
Word : String; {Single word in a string}
begin
Word := '';
if not HaveMoreWords then GetNextWord := '';
while (not (Words[offset] in Seperators)) and (offset <= Length(Words)) do
begin
Word := Word + Words[offset];
inc(Offset);
end;
GetNextWord := word;
end;
end.