TStringList.Insert

From Soldat Community Wiki
Jump to: navigation, search
procedure Insert(Index: Integer; S: string)
 Index: index of Strings[]
 S: string to be inserted

Description

Insert will insert the string S at position Index in the list.
If the list is sorted, an exception will occur instead.
Index is a zero-based position; If Index is below 0 or higher than Count, an exception occurs.

Example

var
  s: TStringList;

begin
  s := File.CreateStringList();
  s.Append('a');
  s.Append('b');
  s.Append('c');

  WriteLn(s.CommaText);
  // a,b,c

  s.Insert(1,'new');

  WriteLn(s.CommaText);
  // a,new,c
  
  
  // s.Insert(4,'d'); // ERROR (4 > Count)
  // s.Insert(-1,'.a'); // ERROR (-1 < 0)

  {
  s.Sort();
  s.Sorted := TRUE;
  s.Insert(1,'b'); // ERROR
  }

  s.Free;
end.