StrCat (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example uses an edit control, a label, and a button on a form. When the button is clicked, the label's caption and the edit control's text are combined in a buffer. Then the buffer is displayed in the label's caption.

Code

procedure TForm1.Button1Click(Sender: TObject);
var
  Buffer: PChar;

begin                                                                            
  { 
    Allocate enough space for the two concatenated strings, plus the trailing zero.
    We take the lengths of both strings, add one for the tariling zero and multiply
    by the size of a character (which is 2 bytes).
  }
  GetMem(Buffer, SizeOf(Char) * (Length(Label1.Caption) + Length(Edit1.Text) + 1));
  
  { Copy the first string into the new buffer }
  StrCopy(Buffer, PChar(Label1.Caption));     
  
  { Append the second string to the buffer }
  SysUtils.StrCat(Buffer, PChar(Edit1.Text));
  
  Label1.Caption := Buffer;
  Edit1.Clear;
  FreeMem(Buffer);
end;

Uses