//tstringlist Common Methods and properties:varList:tstringlist; I:integer;beginList: = tstringlist.create; List.add (' STRINGS1 ');{Add}List.add (' Strings2 '); List.exchange (0,1);{permutation}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 ' aaa,bbb,ccc,ddd ' //equivalent: List.text: = ' aaa ' + #13 # # + ' BBB ' + #13 # # ' + ' CCC ' + ' #13 # # ' + ' DDD '; //4 ShowMessage (list[0//aaa list.free; End;
//Displace delimiter var List:tstringlist; begin ' | ' ' aaa|bbb|ccc|ddd ' //4 ShowMessage (list[0//aaa list.free; End;
//Similar hash table operation methodvarList:tstringlist;beginList: = tstringlist.create; List.add (' aaa=111 '); List.add (' bbb=222 '); List.add (' ccc=333 '); List.add (' ddd=444 '); ShowMessage (list.names[1]);//bbbShowMessage (list.valuefromindex[1]);//222ShowMessage (list.values[' BBB ']);//222//valuefromindex can be assigned values:list.valuefromindex[1] :=' 2 '; ShowMessage (list[1]);//bbb=2//Can be assigned by values:list.values[' BBB '] :=' A '; ShowMessage (list[1]);//bbb=22List.free;End;
//Avoid duplicate values var List:tstringlist; begin List: = tstringlist.create; List.add (' AAA'///need to specify sort first / /If duplicate value is discarded list.add (' aaa '//aaa// Duplicates has 3 selectable values://dupignore: Abort; //dupaccept: End; //duperror: Prompt error. List.free; End;
//Sort and reverse sort{sort Function}functionDesccomparestrings (list:tstringlist; Index1, Index2:integer): Integer;beginResult: =-ansicomparetext (List[index1], list[index2]);End;procedureTform1.button1click (Sender:tobject);varList:tstringlist;beginList: = tstringlist.create; List.add (' BBB '); List.add (' CCC '); List.add (' AAA ');//Not sortedShowMessage (List.text);//BBB CCC AAA//SortList.sort; ShowMessage (List.text);//AAA BBB CCC//Inverted sortList.customsort (desccomparestrings);//Call sort functionShowMessage (List.text);//CCC BBB AAA//if:list.sorted: = True; List.add (' 999 '); List.add (' a '); List.add (' zzz '); ShowMessage (List.text);//000 999 AAA BBB CCC zzzEnd;
Cond...
Tstringlist Common operations