Some advanced usages of the Tstrings class

Source: Internet
Author: User

Tstrings is an abstract class that, in actual development, is the most applied except for the basic type.
General usage We all know, now to discuss some of its advanced usage.
Let's start by listing several properties to be discussed:
1, CommaText
2, Delimiter & Delimitedtext
3, Names & Values & Valuefromindex

Let's look at the first one: CommaText. How to use it? To speak in code:
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, you can see that showmessage shows the difference: AAA BBB CCC DDD.
In other words, strs.commatext: = Constr The function of this sentence is to put a string with ', ' as the delimiter, the segment is added to the tstrings.
So what if we don't split it with ', '? Now 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 displayed effect is identical to the first example. Explain:
Delimiter is the delimiter, the default is: ', '. Delimitedtext is to press delimiter as a string of delimiters, to get the assignment back to the string by delimiter character to add to the tstrings.
Speaking of which, there is an attribute that reminds of QuoteChar. The default value is: ' "' (not including single quotes)
What's the use of it? See 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
The display is still AAA BBB CCC DDD. Why not: "AAA" "BBB" "CCC" "DDD"?
Let's look at one more 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
Displayed is also AAA BBB CCC DDD. It should be easy to compare, you know? This is not much to say, not much to use.
But to say more, when delimiter for: ', ' and QuoteChar: ' ', delimitedtext and Commatext are equal.
The last three to say 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 through this example:
At this time, the content in STRs is:
0=aaa
1=bbb
2=ccc
3=ddd
In names, it is:
0
1
2
3
In the values, it is:
Aaa
Bbb
Ccc
Ddd

Seems a bit inappropriate, but no one can deny that Delphi is good use, this is the last two days in the solution of problems encountered

Delphi Tstringlist Usage Detailed

Tstringlist Common Methods and properties:
Var
List:tstringlist;
I:integer;
Begin
List: = tstringlist.create;
List.add (' Strings1 '); Add
List.add (' Strings2 ');
List.exchange (0,1); Replacement
List.insert (0, ' Strings3 '); Insert
I: = List.indexof (' Strings1 '); {The first occurrence of a position}
List.sort; Sort
list.sorted: = True; {Specify sort}
List.count; Total
List.text; {Text Collection}
List.delete (0); {Delete, 0 is the first data}
List.loadfromfile (' C:\tmp.txt '); Open
List.savetofile (' C:\tmp.txt '); Save
List.clear; Empty
List.free; Release
End


--------------------------------------------------------------------------------

Read in string
Var
List:tstringlist;
Begin
List: = tstringlist.create;
List.commatext: = ' aaa,bbb,ccc,ddd ';
Equivalent: List.text: = ' aaa ' + #13 # # + ' BBB ' + #13 # # ' + ' CCC ' + ' #13 # # ' + ' DDD ';

ShowMessage (IntToStr (List.count)); 4
ShowMessage (List[0]); Aaa

List.free;
End


--------------------------------------------------------------------------------

Displacement delimiter
Var
List:tstringlist;
Begin
List: = tstringlist.create;
List.delimiter: = ' | ';
List.delimitedtext: = ' aaa|bbb|ccc|ddd ';

ShowMessage (IntToStr (List.count)); 4
ShowMessage (List[0]); Aaa

List.free;
End


--------------------------------------------------------------------------------

A similar hash table operation method
Var
List:tstringlist;
Begin
List: = tstringlist.create;

List.add (' aaa=111 ');
List.add (' bbb=222 ');
List.add (' ccc=333 ');
List.add (' ddd=444 ');

ShowMessage (List.names[1]); Bbb
ShowMessage (list.valuefromindex[1]); 222
ShowMessage (list.values[' BBB '); 222

Valuefromindex can be assigned values:
LIST.VALUEFROMINDEX[1]: = ' 2 ';
ShowMessage (list[1]); bbb=2

Values can be assigned by:
list.values[' BBB ']: = ' 22 ';
ShowMessage (list[1]); Bbb=22

List.free;
End

--------------------------------------------------------------------------------
Avoid duplicate values
Var
List:tstringlist;
Begin
List: = tstringlist.create;

List.add (' AAA ');

list.sorted: = True; You need to specify a sort first
List.duplicates: = Dupignore; Discard if duplicate value is found

List.add (' AAA ');

ShowMessage (List.text); Aaa

The duplicates has 3 selectable values:
Dupignore: Give up;
Dupaccept: End;
Duperror: Prompt error.

List.free;
End


--------------------------------------------------------------------------------

Sort and Reverse sort
{Sort Function}
function Desccomparestrings (list:tstringlist; Index1, Index2:integer): Integer;
Begin
Result: =-ansicomparetext (List[index1], list[index2]);
End

Procedure Tform 1.button1click (sender:tobject);
Var
List:tstringlist;
Begin
List: = tstringlist.create;

List.add (' BBB ');
List.add (' CCC ');
List.add (' AAA ');

Not sorted
ShowMessage (List.text); BBB CCC AAA

Sort
List.sort;
ShowMessage (List.text); AAA BBB CCC

Inverted sort
List.customsort (desccomparestrings); Call sort function
ShowMessage (List.text); CCC BBB AAA

If:
list.sorted: = True;
List.add (' 999 ');
List.add (' 000 ');
List.add (' zzz ');
ShowMessage (List.text); 999 AAA BBB CCC ZZZ
End

Some advanced usages of the Tstrings class

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.