Difference between revisions of "TStringList.Insert"

From Soldat Community Wiki
Jump to: navigation, search
(new page)
 
m (Description: typo)
 
Line 6: Line 6:
 
''Insert'' will insert the string ''S'' at position ''Index'' in the list. <br>
 
''Insert'' will insert the string ''S'' at position ''Index'' in the list. <br>
 
If the list is sorted, an  exception will occur instead. <br>
 
If the list is sorted, an  exception will occur instead. <br>
''Index'' is a zero-based position; If Index is below 0 or higher than [[TStringList.Count|Count]], en exception occurs.
+
''Index'' is a zero-based position; If Index is below 0 or higher than [[TStringList.Count|Count]], an exception occurs.
  
 
==Example==
 
==Example==

Latest revision as of 12:26, 24 August 2013

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.