TStringList.Add

From Soldat Community Wiki
Jump to: navigation, search
function Add(S: string): Integer
 S: string to be added
 Result: Strings[] index with newly added string

Description

This function will add S to the list. Note that Duplicates affects the function.
If the list is sorted and the string S is already present in the list and Duplicates is dupError then an error occurs.
If Duplicates is set to dupIgnore then the return value is underfined.

If the list is sorted, new strings will not necessarily be added to the end of the list, rather they will be inserted at their alphabetical position.

Example

var
  s: TStringList;

begin
  s := File.CreateStringList();
  s.Add('b');
  s.Add('a');
  s.Add('d');
  WriteLn(s.Text);
{
b
a
d

}

  s.Add('c');
  WriteLn(s.Text);
{
b
a
d
c

}
  
  s.Sort;
  s.Sorted := TRUE; 
  // necessary if you want further values to be sorted as well
  s.Duplicates := dupIgnore;
  
  WriteLn(s.Text);
{
a
b
c
d

}
  
  
  s.Add('c'); // this will be ignored (s.Duplicates = dupIgnore)
  s.Add('aa');
  
  WriteLn(s.Text);
{
a
aa
b
c
d

}
  
  s.Free;
end.