Create a copy of part of a string or an array System unit
1  function Copy ( Source : string; StartChar, Count : Integer ) : string;
2  function Copy ( Source : array; StartIndex, Count : Integer ) : array;
The Copy function has 2 forms. In the first, it creates a new string from part of an existing string. In the second, it creates a new array from part of an existing array.
1.String copy
The first character of a string has index = 1.
Up to Count characters are copied from the StartChar of the Source string to the returned string.
Less than Count characters if the end of the Source string is encountered before Count characters have been copied.
2.Array copy
The first element of an array has index = 0.
Up to Count elements are copied from the StartIndex of the Source array to the returned array.
Less than Count elements if the end of the Source array is encountered before Count elements have been copied.
For string copying, a StartChar less than 1 is treated as 1.

To guarantee copying to the end of the string or array, use the MaxInt constant as the Count value.

When copying multi-dimensional arrays, only the first dimension is copied. The elements of all but the final dimension of an array are all pointers to sub-arrays that make up the whole array. After the copy, the target array elements continue to point to the sub-arrays of the source array. These sub-arrays are thereby shared by both the source and target arrays. You have been warned!
Related commands
AnsiReplaceStr Replaces a part of one string with another
Concat Concatenates one or more strings into one string
Delete Delete a section of characters from a string
Insert Insert a string into another string
Move Copy bytes of data from a source to a destination
StringOfChar Creates a string with one character repeated many times
StringReplace Replace one or more substrings found within a string
StuffString Replaces a part of one string with another
WrapText Add line feeds into a string to simulate word wrap
 Author links

 Buy Website Traffic at

 Buy Proxies at
 Download this web site as a Windows program.

Example code : String copy
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
unit Unit1;
  // The System unit does not need to be defined
  Forms, Dialogs;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
Form1: TForm1;
{$R *.dfm} // Include form definitions
procedure TForm1.FormCreate(Sender: TObject);

  Source, Target : string;

  Source := '12345678';
  Target := Copy(Source, 3, 4);
  ShowMessage('Target : '+Target);
Hide full unit code
   Target : 3456
Example code : Array copy
  i : Integer;
  Source, Target : array of Integer;

  SetLength(Source, 8);

  for i := 1 to 8 do   // Build the dynamic source array
    Source[i-1] := i;   // Remember that arrays start at index 0

  Target := Copy(Source, 3, 4);

  for i := 0 to Length(Target) -1 do   // Display the created array
    ShowMessage('Target['+IntToStr(i)+'] : '+IntToStr(Target[i]));
Show full unit code
   Target[0] : 4
   Target[1] : 5
   Target[2] : 6
   Target[3] : 7
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page