Functions used to split strings in Delphi

Source: Internet
Author: User

Unit classes

Syntax

[Delphi] function extractstrings (separators: tsyscharset; whitespace: tsyscharset; content: pansichar; strings: tstrings): integer;

Description use extractstrings to fill a string list with the substrings of the null-terminated string specified by content.

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 .)

Whitespace is a set of characters to be ignored when parsing Content if they occur at the beginning of a string.

Content is the null-terminated string to parse into substrings.

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. extractstrings returns the number of strings added to the strings parameter. note:
Extractstrings does not add empty strings to the list.
 

[Update]:
The separators parameter specifies a group of delimiters. All substrings are separated by them. However, the separators in the pair quotation marks are ignored (see the example below ).
The whitespace parameter specifies the character s that is ignored at the beginning of each substring.
The content parameter is the split "Source" string.
The strings parameter is used to receive split substrings. Its original content will not be cleared. Don't forget to create.
In addition, ectractstrings do not add null strings (after whitespaces is ignored) to strings.

Write an example:
For example
ABC |... Def | #### Ghi | "not separated | # www.farproc.com"
To get
ABC
Def
Ghi
Not separated | # www.farproc.com
The following code can be used for the four substrings:

Uses
Classes;
VaR
Asource: pchar;
Astr: string;
Acount: integer;
Astrings: tstringlist;
Begin
Asource: = 'abc |... Def | #### Ghi | "not separated | # 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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.