TstringsIs 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? UseCodeSpeaker:
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