Embarcadero Delphi TStringList class Key-Value Pairing


Example

You can use a TStringList to store Key-Value pairs. This can be useful if you want to store settings, for example. A settings consists of a Key (The Identifier of the setting) and the value. Each Key-Value pair is stored in one line of the StringList in Key=Value format.

procedure Demo(const FileName: string = '');
var
   SL: TStringList;
   i: Integer;
begin
     SL:= TStringList.Create;
     try
        //Adding a Key-Value pair can be done this way
        SL.Values['FirstName']:= 'John';   //Key is 'FirstName', Value is 'John'
        SL.Values['LastName']:= 'Doe';   //Key is 'LastName', Value is 'Doe'

        //or this way
        SL.Add('City=Berlin');  //Key ist 'City', Value is 'Berlin'

        //you can get the key of a given Index
        IF SL.Names[0] = 'FirstName' THEN
         begin
              //and change the key at an index
              SL.Names[0]:= '1stName';  //Key is now "1stName", Value remains "John"
         end; 
      
        //you can get the value of a key
        s:= SL.Values['City']; //s now is set to 'Berlin'

        //and overwrite a value 
        SL.Values['City']:= 'New York';

        //if desired, it can be saved to an file
        IF (FileName <> '') THEN
         begin
              SL.SaveToFile(FileName);
         end;
     finally
        SL.Free;
     end;
end;

In this example, the Stringlist has the following content before it is destroyed:

1stName=John
LastName=Doe
City=New York

Note on performance

Under the hood TStringList performs key search by straight looping through all items, searching for separator inside every item and comparing the name part against the given key. No need to say it does huge impact on performance so this mechanism should only be used in non-critical, rarely repeated places. In cases where performance matters, one should use TDictionary<TKey,TValue> from System.Generics.Collections that implements hash table search or to keep keys in sorted TStringList with values stored as Object-s thus utilizing binary find algorithm.