Delphi分割字符串的函数--ExtractStrings


本文整理自网络,侵删。

 博主一直以为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.

相关阅读 >>

Delphi判断线程是否正在运行

Delphi yesterday、today、tomorrow - 昨天、今天、明天

Delphi随机字符(密码生成)函数

Delphi字符串、数组操作函数

Delphi 怎么截取文件路径字符串,只保留文件名

Delphi里的compile和build都能产生可执行文件,有什么区别啊?

Delphi spcomm控件的安装

Delphi中提取网址链接分路径

Delphi xe 泛型数组和splitstring处理数据

Delphi搜索字符串在流中的位置

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...