Tstringlist Common operations
//Tstringlist Common Methods and properties:VarList:tstringlist; I:integer;BeginList: =Tstringlist.create; List.add (‘Strings1‘);{Add to}List.add (‘Strings2‘); List.exchange (0,1);{Replacement}List.insert (0,‘Strings3‘);{Insert}I: = List.indexof (‘Strings1‘);{The first occurrence of the 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 it}List.savetofile (‘C:\tmp.txt‘);{Save}List.clear;{Empty}List.free;{Release}End;--------------------------------------------------------------------------------//Read in stringVarList:tstringlist;BeginList: =Tstringlist.create; List.commatext: =‘Aaa,bbb,ccc,ddd‘; Equivalent: List.text: =‘Aaa+13#10 +‘Bbb+13#10‘+' CCC‘+‘#13#10‘+' DDD‘;ShowMessage (IntToStr (List.count)); //4ShowMessage (list[0]); //AAA List.free;End;--------------------------------------------------------------------------------//Displacement delimiterVarList:tstringlist;BeginList: =Tstringlist.create; List.delimiter: =‘|‘; List.delimitedtext: =‘Aaa|bbb|ccc|ddd‘; ShowMessage (IntToStr (List.count)); //4ShowMessage (list[0]); //AAA List.free;End;--------------------------------------------------------------------------------//A 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‘); List.add (‘Eee=‘); List.add (‘Fff‘); ShowMessage (list.names[1]); //BBB ShowMessage (list.valuefromindex[1]); //222ShowMessage (list.values[‘Bbb‘]); //222//Valuefromindex can be assigned a value: list.valuefromindex[1]: =‘2‘; ShowMessage (list[1]); bbb=2//Values can be assigned by: list.values[‘Bbb']: =‘22‘; ShowMessage (list[1]); bbb=22ShowMessage (list.names[4]); //Eee ShowMessage (list.valuefromindex[4]); //ShowMessage (list.values[‘Eee‘]); //ShowMessage (list.names[5]); //ShowMessage (list.valuefromindex[5]); //ShowMessage (list.values[‘Fff‘]); //List.free;End;--------------------------------------------------------------------------------//Avoid duplicate valuesVarList:tstringlist;BeginList: =Tstringlist.create; List.add (‘Aaa‘); list.sorted: = True; //The sort list.duplicates needs to be specified first: = Dupignore; //If there is a duplicate value, discard the List.add (‘Aaa‘); ShowMessage (List.text); //AAA//Duplicates has 3 selectable values://Dupignore: Give up; //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 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); 000 999 AAA BBB CCC ZZZ end;
Tstringlist Common actions