Unit
Classes
Syntax
ExtractStrings(Separators: TSysCharSet; WhiteSpace: TSysCharSet; Content: PAnsiChar; Strings: TStrings): Integer;
Description < by specified null-terminated the of fill to ExtractStrings>
< is character quote if quoted in appear can characters that (Note quote. end final until inside when ignored are Separators separators. as treated always double) or (single and characters, newline returns, Carriage substrings. separating delimiters, used set> < beginning at occur they Content parsing be> < into parse> < already strings any so ExtractStrings, cleared not The added. all which>< Strings added number returns>
ExtractStrings does not add empty strings to the list
[英文看着比较累,还是中文一下吧]: WhiteSpace 参数指定每个子串开头被忽略的字符s。
Content 参数就是被分割的“源”串了。
Strings 参数用于接收分割后的各个子串。它的原有内容不会被清空。别忘了对Strings进行Create哦。
另外,EctractStrings不会把空串放入Strings中去。
举个例子吧:
如果我想分割以下字符串: ABC|DEF|GHI
希望得到下面四个字符串:
1、ABC
2、DEF
3、GHI
那么可以用下面的代码:
一:直接使用 ExtractStrings函数
- uses Classes;
- var
- ASource: PChar;
- AStr: String;
- ACount,i: Integer;
- AStrings: TStringList;
- begin
- ASource := 'ABC|DEF|GHI';
- AStrings := TStringList.Create;
- try
- ACount := ExtractStrings(['|'], [' ','#','.'], ASource, AStrings);
- For i to ACount-1 do
- begin
- ShowMessage(AStrings[i]); //i=0:ABC;i=1:DEF;i=2:GHI
- end;
- //........................
- finally
- AStrings.Free;
- end;
- end.
二:动态赋值分隔符方法
var
ASource: string;
i: Integer;
AStrings: TStringList;
Delimiter: Char; // 分隔符
begin
try
ASource := 'ABC|DEF|GHI';
Delimiter := '|';
AStrings := TStringList.Create;
AStrings.Delimiter := Delimiter;
AStrings.DelimitedText := PChar(ASource);
for i := 0 to AStrings.Count - 1 do
ShowMessage(AStrings.Strings[i]);
finally
AStrings.Free;
end;
end;