<basefont>ExtractStrings Routine <descrshort id="descrShort" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><span class="para">Fills a string list with substrings parsed from a delimited list.</span></descrshort>
Unit <link keywords="Classes_Win32_Delphi" indexmoniker="!DefaultAssociativeIndex" namespace="ms-help://borland.bds4">
Syntax
[Delphi] function ExtractStrings(Separators: TSysCharSet; WhiteSpace: TSysCharSet; Content: PAnsiChar; Strings: TStrings): Integer;
Description <descrlong id="descrLong" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><span class="para">Use ExtractStrings to fill a string list with the substrings of the null-terminated string specified by Content.</span> <br></descrlong><descrlong id="descrLong" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><span class="para">Separators is a set of characters that are used as delimiters, separating the substrings. Carriage returns, newline characters, and quote characters (single or double) are always treated as separators. Separators are ignored when inside a quoted string until the final end quote. (Note that quoted characters can appear in a quoted string if the quote character is doubled.)</span> <span class="para"><br></span></descrlong>
<descrlong id="descrLong" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><span class="para">WhiteSpace is a set of characters to be ignored when parsing Content if they occur at the beginning of a string.</span> <span class="para"><br></span></descrlong>
<descrlong id="descrLong" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><span class="para">Content is the null-terminated string to parse into substrings.</span> <span class="para"><br></span></descrlong>
<descrlong id="descrLong" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><span class="para">Strings is a string list to which all substrings parsed from Content are added. The string list is not cleared by ExtractStrings, so any strings already in the string list are preserved.</span> <span class="para">ExtractStrings returns the number of strings added to the Strings parameter.</span> </descrlong>
|
Note: |
ExtractStrings does not add empty strings to the list. |
Separators 参数指定一组分割符,所有的子串都是用它们分割的。但是成对的引号内的分割符会被忽略(参看下面的例子)。
WhiteSpace 参数指定每个子串开头被忽略的字符s。
Content 参数就是被分割的“源”串了。
Strings 参数用于接收分割后的各个子串。它的原有内容不会被清空。别忘了Create哦。
另外,EctractStrings不会把(忽略WhiteSpaces后的)空串加入到Strings中。
写个例子吧:
比如
ABC|... DEF|#### GHI|"不会被分开|# www.farproc.com"
要得到
ABC四个子串可以用下面的代码:
DEF
GHI
不会被分开|# www.farproc.com
uses
Classes;
var
ASource: PChar;
AStr: String;
ACount: Integer;
AStrings: TStringList;
begin
ASource := 'ABC|... DEF|#### GHI|"不会被分开|# www.farproc.com"';
AStrings := TStringList.Create;
try
ACount := ExtractStrings(['|'], [' ', '#', '.'], ASource, AStrings);
{do any further processing}
/for AStr in AStrings do
// Writeln(AStr);
finally
AStrings.Free;
end;
Readln;
end.
本文介绍了Delphi中内置的ExtractStrings函数,该函数能够高效地进行字符串分割,并去除空白和空字符串。通过实例展示了如何使用此函数进行复杂的字符串解析。
1121

被折叠的 条评论
为什么被折叠?



