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
Variant
s, in which case they are properly
initialized.
SetLength
is not a real procedure.
Tips and Tricks
The
Length
is in logical units (array elements, characters). In particular, ifS
is aWideString
, theLength
is in characters, not bytes.If the string or array has multiple references,
SetLength
always creates a unique instance and sets its length, even if the new length is the same as the current length.If
A
is 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); ...
Get Delphi in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.