主页 > 编程资料 > Delphi >
发布时间:2015-09-22 作者:网络 阅读:156次

博主一直以为Delphi中没有分割字符串的函数,在此之前曾经自己写过一个SplitStrings,一直使用至今,但在一次翻阅Delphi帮助时,无意中在Classes单元中发现Delphi本身就有分割字符串的函数--ExtractStrings,而且功能还不弱,分割的同时还可以去空字符串和去空白(可以自定义)。反正比我写的好啊!^_^

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.



[E文看着比较累,还是中文一下吧]: WhiteSpace 参数指定每个子串开头被忽略的字符s。
Content 参数就是被分割的“源”串了。
Strings 参数用于接收分割后的各个子串。它的原有内容不会被清空。别忘了对Strings进行Create哦。
另外,EctractStrings不会把空串放入Strings中去。

举个例子吧:

如果我想分割以下字符串: ABC|... DEF|#### GHI|"中华网URL

希望得到下面四个字符串:
1、ABC
2、DEF
3、GHI
4、中华网URL
那么可以用下面的代码:
view plaincopy to clipboardprint?

uses Classes;
var
ASource: PChar;
AStr: String;
ACount: Integer;
AStrings: TStringList;
begin
ASource := 'ABC|... DEF|#### GHI|"中华网URL';
AStrings := TStringList.Create;
try
ACount := ExtractStrings(['|'], [' ','#','.'], ASource, AStrings);
//........................
finally
AStrings.Free;
end;
end.
关键字词: