Difference between revisions of "TStringList"

From Lazarus wiki
Jump to navigationJump to search
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
A '''TStringList''' is a datatype that can hold an arbitrary length list of [[String|strings]]. The strings in a TStringList are accessible as concatenated plain text or as a series of strings.
+
{{TStringList}}
  
== Inheritance ==
+
A '''TStringList''' is a [[Data type|datatype]] that can hold an arbitrary length list of [[String|strings]]. The strings in a TStringList are accessible as concatenated plain text or as a series of strings. Functionality is also provided for key-value pair access.
*[[TObject]] - Base class of all classes.
+
 
 +
;Inheritance
 +
*[[TObject]] - Base [[Class|class]] of all classes.
 
**[[TPersistent]], [[IFPObserved]] - Base class for streaming system and persistent properties - Interface implemented by an object that can be observed.
 
**[[TPersistent]], [[IFPObserved]] - Base class for streaming system and persistent properties - Interface implemented by an object that can be observed.
 
***[[TStrings]] - Class to manage arrays or collections of strings
 
***[[TStrings]] - Class to manage arrays or collections of strings
 
****'''TStringList''' - Standard implementation of the TStrings class.
 
****'''TStringList''' - Standard implementation of the TStrings class.
 +
 +
TStringList adds sorting functionality to [[TStrings]] by adding properties <tt>Sorted</tt>, <tt>Duplicates</tt> and [[case-sensitive|<tt>CaseSensitive</tt>]] and [[Method|methods]] like <tt>Find</tt> to facilitate speeded search within a list.
 +
 +
;Example
 +
<syntaxhighlight lang="pascal">
 +
  // get a value from file FILNAM filled with key=value pairs
 +
function GetValueFromFile( filnam: string, key: string ): string;
 +
var
 +
  lst: TStringList;
 +
  v: String;
 +
begin
 +
  lst := TStringList.Create();
 +
  lst.CaseSensitive := false;
 +
  lst.Duplicates := dupIgnore; // do not add duplicates
 +
  lst.Sorted := true;
 +
  lst.LoadFromFile( filnam );
 +
  v := lst.Values[ key ];
 +
  lst.Free();
 +
  result := v;
 +
end;
 +
</syntaxhighlight>
 +
 +
TStringlist also has a 'builtin' data dictionary mode, here is an example provided by forum user Remy Lebeau
 +
 +
    uses
 +
      ..., Classes;
 +
   
 +
    var
 +
      SL: TStringList;
 +
      Value: Integer;
 +
    begin
 +
      SL := TStringList.Create;
 +
      try
 +
        SL.Add('VarName=99');
 +
        ...
 +
        Value := StrToInt(SL.Values['VarName']);
 +
        ...
 +
      finally
 +
        SL.Free;
 +
      end;
 +
    end;
 +
  
 
==See also==
 
==See also==
 
* [[doc:rtl/classes/tstringlist.html|TStringList doc]]
 
* [[doc:rtl/classes/tstringlist.html|TStringList doc]]
* [[TStringList-TStrings Tutorial]]  
+
* [[TStringList-TStrings Tutorial]]
 
+
* [[TBufStream | example of saving a TStringList to disk faster than the SaveToFile method can]]
[[Category:RTL]]
 
[[Category:Containers]]
 

Latest revision as of 16:02, 29 August 2021

English (en) suomi (fi) polski (pl)

A TStringList is a datatype that can hold an arbitrary length list of strings. The strings in a TStringList are accessible as concatenated plain text or as a series of strings. Functionality is also provided for key-value pair access.

Inheritance
  • TObject - Base class of all classes.
    • TPersistent, IFPObserved - Base class for streaming system and persistent properties - Interface implemented by an object that can be observed.
      • TStrings - Class to manage arrays or collections of strings
        • TStringList - Standard implementation of the TStrings class.

TStringList adds sorting functionality to TStrings by adding properties Sorted, Duplicates and CaseSensitive and methods like Find to facilitate speeded search within a list.

Example
  // get a value from file FILNAM filled with key=value pairs 
function GetValueFromFile( filnam: string, key: string ): string;
var
  lst: TStringList;
  v: String;
begin
  lst := TStringList.Create();
  lst.CaseSensitive := false;
  lst.Duplicates := dupIgnore; // do not add duplicates
  lst.Sorted := true;
  lst.LoadFromFile( filnam );
  v := lst.Values[ key ];
  lst.Free();
  result := v;
end;

TStringlist also has a 'builtin' data dictionary mode, here is an example provided by forum user Remy Lebeau

   uses
     ..., Classes;
    
   var
     SL: TStringList;
     Value: Integer;
   begin
     SL := TStringList.Create;
     try
       SL.Add('VarName=99');
       ...
       Value := StrToInt(SL.Values['VarName']);
       ...
     finally
       SL.Free;
     end;
   end;


See also