Delphi Basics
SetString
Procedure
Copies characters from a buffer into a string System unit
 procedure SetString ( var TargetString : string; BufferPointer : PChar; Length : Integer ) ;
Description
The SetString procedure sets the length of TargetString to Length before copying that number of characters from the buffer referenced by BufferPointer.
 
The length is only set when the string is not a ShortString. In fact, the string is reallocated - the TargetString reference is then set to point to this new string.
Related commands
FillChar Fills out a section of storage with a fill character or byte value
SetLength Changes the size of a string, or the size(s) of an array
StringOfChar Creates a string with one character repeated many times
 
Example code : A simple example
// 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;
 
interface
 
uses
  // The System unit does not need to be defined
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  target : string;
  source : array[1..5] of Char;
  srcPtr : PChar;
  i      : Integer;

begin
  // Fill out the character array
  for i := 1 to 5 do
    source[i] := Chr(i+64);

  // Copy these characters to a string
  srcPtr := Addr(source);
  SetString(target, srcPtr, 5);

  // Show what we have got
  ShowMessage('target now = '+target);
end;
 
end.
Hide full unit code
   target now = ABCDE
 
 
© CodeGear 2006 - 2007. All rights reserved.  |  Home Page