Name
SetString Procedure
Syntax
procedure SetString(var Str: string; Buffer: PChar; Length: Integer);
Description
The SetString
procedure sets the length of
Str
to Length
, then copies
Length
characters from Buffer
to the string. SetString
is not a real
procedure.
Tips and Tricks
If
Str
is a long string,SetString
always allocates a new string, soStr
is set to a unique string.Str
can be aShortString
, in which case theLength
must be less than 256.Buffer
can benil
, in which case the string length is set, but its contents are left uninitialized. In the case of aShortString
, the previous string contents are left untouched.
Example
// Given an instance or module handle, return the corresponding
// filename. Note that Delphi will automatically convert
// a PChar to a string, but it must scan the PChar to learn
// its length. GetModuleFileName returns the length, so there is
// no need to let Delphi rescan the string.
function GetModuleName(Instance: THandle): string;
var
Len: Integer;
Buffer: array[0..MAX_PATH] of Char;
begin
Len := GetModuleFileName(HInstance, Buffer, SizeOf(Buffer));
SetString(Result, Buffer, Len);
end;
See Also
AnsiString Type, SetLength Procedure, ShortString Type, String Keyword, WideString Type |
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.