How to separate strings by given characters in Delphi (similar to the split function)

Source: Internet
Author: User

The occasional Delphi program involved in string processing, which has a function similar to the split () function in VB, so I checked some information for a long time, now we will sort out these materials for your convenience.

First, a user compiled a function to implement the function and split.

Unit unit1;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;

Type userarray = array of string;

Type
Tform1 = Class (tform)
Edit1: tedit;
Button1: tbutton;
Procedure button1click (Sender: tobject );
Private
Function split (S: string; dot: Char): userarray;
{Private Declarations}
Public
{Public declarations}
End;

VaR
Form1: tform1;

Implementation

Uses strutils;

{$ R *. DFM}
// Separate strings into arrays based on the given characters
Function tform1.split (S: string; dot: Char): userarray;
VaR
STR: userarray;
I, J: integer;
Begin
I: = 1;
J: = 0;
Setlength (STR, 255 );

While pos (dot, s)> 0 do // POS returns the position where the substring first appears in the parent string.
Begin
STR [J]: = copy (S, I, pos (dot, S)-I );
I: = pos (dot, S) + 1;
S [I-1]: = CHR (ord (DOT) + 1 );
J: = J + 1;
End;
STR [J]: = copy (S, I, strlen (pchar (s)-I + 1 );
Result: = STR;
End;

Procedure tform1.button1click (Sender: tobject );
VaR
UR: userarray;
I: integer;
Begin
UR: = Split (edit1.text ,';');
For I: = 0 to 255 do
Begin
If length (UR [I]) = 0 Then exit;
Showmessage (UR [I]);
End;

End;

End.

Note: When testing this code, please put a text edit box and a button on the form. The string is separated by the ';' sign;

The second method is relatively simple:

Tstringlist usage
Tstrings is an abstract class. In actual development, it has the most applications except the basic type.
We all know the general usage. Now we will discuss some of its advanced usage.
First, list the several attributes to be discussed:
1. commatext
2. delimiter & delimitedtext
3. Names & Values & valuefromindex
First read: commatext. How to use it? Use code to speak:
Const
Constr: String = 'aaa, BBB, CCC, DDD ';
VaR
STRs: tstrings;
I: integer;
Begin
STRs: = tstringlist. Create;
STRs. commatext: = constr;
For I: = 0 to STRs. Count-1 do
Showmessage (STRs [I]);
End;
After executing this code, we can see that the showmessage displays aaa bbb ccc ddd.
That is to say, the role of STRs. commatext: = constr is to add a string to tstrings with the separator ',' and segments.
So what should we do if it is not separated? Now let's look at the second example. Use delimiter and delimitedtext.
Const
Constr: String = 'aaa \ BBB \ CCC \ DDD ';
VaR
STRs: tstrings;
I: integer;
Begin
STRs: = tstringlist. Create;
STRs. delimiter: = '\';
STRs. delimitedtext: = constr;
For I: = 0 to STRs. Count-1 do
Showmessage (STRs [I]);
End;
As you can see, the display effect is exactly the same as that in the first example. Explanations:
Delimiter is the delimiter. The default value is :','. Delimitedtext is a string separated by delimiter. After a value is assigned, the string is added to tstrings by delimiter.
Here, I think of a property, quotechar. The default value is '"' (excluding single quotation marks)
What is the purpose? Example:
Const
Constr: String = '"AAA" \ "BBB" \ "CCC" \ "DDD "';
VaR
STRs: tstrings;
I: integer;
Begin
STRs: = tstringlist. Create;
STRs. delimiter: = '\';
STRs. delimitedtext: = constr;
For I: = 0 to STRs. Count-1 do
Showmessage (STRs [I]);
End;
It is still aaa bbb ccc ddd. Why not: "AAA" "BBB" "CCC" "DDD?
Let's look at an example:
Const
Constr: String = '| AAA | \ | BBB |\| CCC |\| DDD | ';
VaR
STRs: tstrings;
I: integer;
Begin
STRs: = tstringlist. Create;
STRs. delimiter: = '\';
STRs. quotechar: = '| ';
STRs. delimitedtext: = constr;
For I: = 0 to STRs. Count-1 do
Showmessage (STRs [I]);
End;
Aaa bbb ccc ddd is displayed. In comparison, it is not difficult to understand it, right? This is not much to say, and it is not used much.
But let's say that when Delimiter is: ',' and quotechar is: '"', delimitedtext and commatext are the same.
The last three parts are names & Values & valuefromindex.
Take a look at the following code:
Const
Constr: String = '0 = aaa, 1 = BBB, 2 = CCC, 3 = DDD ';
VaR
STRs: tstrings;
I: integer;
Begin
STRs: = tstringlist. Create;
STRs. commatext: = constr;
For I: = 0 to STRs. Count-1 do
Begin
Showmessage (STRs. Names [I]);
Showmessage (STRs. Values [STRs. Names [I]);
Showmessage (STRs. valuefromindex [I]);
End;
End;
It is not difficult to see from this example:
In this case, the content in STRs is:
0 = aaa
1 = bbb
2 = ccc
3 = ddd
While names is:
0
1
2
3
In values, it is:
Aaa
Bbb
CCC
Ddd

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.