Common tstringlist attributes in Delphi

Source: Internet
Author: User
Common tstringlist attributes in Delphi // Common tstringlist methods and attributes: 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‘ );   {Location of the first appearance}    List . Sort;                       {Sorting}    List . Sorted :=  True ;    {Specified sorting}    List . Count;                      {Total}    List . Text;                       {Text set}    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 the string var    List: TStringList; begin    List := TStringList . Create;    List . CommaText :=  ‘aaa,bbb,ccc,ddd‘ ;    // Equivalent to: list. text: = 'aaa' + #13 #10 + 'bbb '+ #13 # 10' + 'ccc' + '#13 # 10' + 'ddd ';     ShowMessage(IntToStr(List . Count));   //4    ShowMessage(List[ 0 ]);   //aaa     List . Free; end ;  // Replace Separators 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 ; // 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 a value:    List . ValueFromIndex[ 1 ] :=  ‘2‘ ;    ShowMessage(List[ 1 ]);   //bbb=2     // You can assign values through values:    List . Values[ ‘bbb‘ ] :=  ‘22‘ ;    ShowMessage(List[ 1 ]);   //bbb=22     List . Free; end ;  // Avoid repeated values var    List: TStringList; begin    List := TStringList . Create;     List . Add( ‘aaa‘ );     List . Sorted :=  True ;   // Specify the sorting first    List . Duplicates := dupIgnore;   // Give up if there is a duplicate value     List . Add( ‘aaa‘ );     ShowMessage(List . Text);   //aaa     // Duplicates has three optional values:    // Dupignore: give up;    // Dupaccept: end;    // Duperror: an error is prompted.     List . Free; end ; // Sort and reverse sort {Sorting function} function  DescCompareStrings(List: TStringList; Index1, Index2:  Integer ):  Integer ; begin    Result := -AnsiCompareText(List[Index1], List[Index2]); end ;  procedure  TForm1 . Button1Click(Sender: TObject); var    List: TStringList; begin    List := TStringList . Create;     List . Add( ‘bbb‘ );    List . Add( ‘ccc‘ );    List . Add( ‘aaa‘ );     // Unordered    ShowMessage(List . Text);   //bbb ccc aaa     // Sort    List . Sort;    ShowMessage(List . Text);   //aaa bbb ccc     // Inverted sorting    List . CustomSort(DescCompareStrings);   // Call the sorting Function    ShowMessage(List . Text);   //ccc bbb aaa     // Assume that:    List . Sorted :=  True ;    List . Add( ‘999‘ );    List . Add( ‘000‘ );    List . Add( ‘zzz‘ );    ShowMessage(List . Text);   //000 999 aaa bbb ccc zzz end ;

Common tstringlist attributes in Delphi

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.