Difference between revisions of "TStringList"

From Lazarus wiki
Jump to navigationJump to search
Line 19: Line 19:
 
   lst := TStringList.Create();
 
   lst := TStringList.Create();
 
   lst.CaseSensitive := false;
 
   lst.CaseSensitive := false;
   lst.Duplicates := false;
+
   lst.Duplicates := dupIgnore; // do not add duplicates
 
   lst.Sorted := true;
 
   lst.Sorted := true;
   lst.ReadFromFile( filnam );
+
   lst.LoadFromFile( filnam );
 
   v := lst.Values[ key ];
 
   v := lst.Values[ key ];
 
   lst.Free();
 
   lst.Free();

Revision as of 14:13, 23 February 2017

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;

See also