RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.Insert Function

Inserts a substring into a string beginning at a specified point.

Pascal
procedure Insert(Source: string; var S: string; Index: Integer);
C++
Insert(AnsiString Source, AnsiString S, int Index);

System

In Delphi code, Insert merges Source into S at the position S[index]. 

Source is a string-type expression. S is a string-type variable of any length. Index is an integer-type expression. It is a character index and not a byte index. 

If Index is less than 1, it is mapped to a 1. If it is past the end of the string, it is set to the length of the string, turning the operation into an append. 

If the Source parameter is an empty string, Insert does nothing. 

Insert throws an EOutOfMemory exception if it is unable to allocate enough memory to accomodate the new returned string.  

Delphi Examples: 

 

{
This example uses the Insert function to edit a string.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  S: string;
begin
  S := 'Honest Lincoln';
  Canvas.TextOut(10, 10, S);
  Insert('Abe ', S, 8);                    { 'Honest Abe Lincoln' }
  Canvas.TextOut(10, 40, S);
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!