[Overview][Constants][Types][Classes][Procedures and functions][Variables][Index] Reference for unit 'System' (#rtl)

SetLength

Set length of a string.

Declaration

Source position: system.fpd line 78

 procedure SetLength( var S: AStringType; Len: SizeInt );procedure SetLength( var A: DynArrayType; Len: SizeInt );

Description

SetLength sets the length of the string S to Len. S can be an ansistring, a short string or a widestring. For ShortStrings, Len can maximally be 255. For AnsiStrings it can have any value. For AnsiString strings, SetLength \emph{must} be used to set the length of the string.

In the case of a dynamical array A, setlength sets the number of elements. The elements are numbered from index 0, so the count runs from 0 to Len-1. If Zero is specified, the array is cleared.

In case the length is set to a smaller length than the current one, the existing elements (characters in case of a string) are kept.

In case the length is set to a larger length than the current one, the new elements are zeroed out for a dynamic array. For a string, the string is zero-terminated at the correct length.

Note that SetLength is governed by the Copy-On-Write principle for strings and dynamic arrays: the reference count after a call to SetLength will be 1.

Errors

None.

 Length Returns length of a string or array.

Example

Program Example85;

{ Program to demonstrate the SetLength function. }

Var S : String;

begin
Setlength(S,100);
FillChar(S[1],100,#32);
Writeln ('"',S,'"');
end.