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 trimleft 删除字符串左边的空格

Delphi 隐藏 tpagecontrol 的标签方法

Delphi vcl gif 动画

Delphi idhttp中application/x-www-form-urlencoded字符说明

Delphi获取千千静听歌词下载地址源码

Delphi intraweb 在iis下发布的web.config

Delphi 数组与枚举

Delphi sysutils.strcopy、sysutils.strecopy

Delphi 获取文件创建时间,修改时间,最后

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



打赏

取消

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

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

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

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

评论

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