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 简单的软件注册demo

Delphi隐藏进程

Delphi 修改快捷方式

Delphi 通过api 隐藏任务栏时间日期

Delphi tbitmap 位图组件

Delphi 屏幕渐变效果的源代码

Delphi 截取绝对路径的文件名

Delphi 得到 winrar 处理解压缩文件的返回值 ?

Delphi 程序重新启动自身

Delphi indy防止假死

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



打赏

取消

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

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

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

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

评论

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