Usage of Delphi tstringlist

Source: Internet
Author: User
The usage of Delphi tstringlist tstrings is an abstract class. In actual development, tstrings have the most applications besides the basic types.
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. First read the names & Values & valuefromindex: 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.