Name
SetLength Procedure
Syntax
procedure SetLength(var S: string; Length: Integer);
procedure SetLength(var A: Array type; Length: Integer);
procedure SetLength(var A: Array type; Len1: Integer; Len2...);Description
The SetLength procedure changes the size of a
string or dynamic array. If the new length is longer than the current
length, the extra space is not initialized, unless the array contains
strings, interfaces, other dynamic arrays, or
Variants, in which case they are properly
initialized.
SetLength is not a real procedure.
Tips and Tricks
The
Lengthis in logical units (array elements, characters). In particular, ifSis aWideString, theLengthis in characters, not bytes.If the string or array has multiple references,
SetLengthalways creates a unique instance and sets its length, even if the new length is the same as the current length.If
Ais a multidimensional dynamic array, you can pass one or more lengths toSetLength. The first length is for the leftmost dimension, and subsequent lengths initialize successive dimensions. The default length is zero for any dimension that you omit fromSetLength.
Example
// Return a string of length Length, filled with repetitions
// of the given character.
function FillString(Length: Integer; Fill: Char): string;
begin
SetLength(Result, Length); FillChar(Result[1], Length, Fill); end; // Create a square identity matrix of size N. type TIntMatrix = array of array of integer; procedure SetIdentityMatrix(var M: TIntMatrix; N: Integer); ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access