Table of Contents

strings and string lists in Delphi

see also:

introduction

string routines

formatting strings

other routines

TStringList and TStrings

var
  slist: TStringList; 
begin
 
slist := TStringList.Create;
try
  ...
  // do things with your stringlist  
  ...
finally
  if Assigned(slist) then
    FreeAndNil(slist);
end;
end;

TStringBuilder

procedure TMainForm.Button1Click(Sender: TObject);
var
  SB: TStringBuilder;
begin
  { Increase the click count for the button. }
  Inc(FClickCount);
 
  { Create a new instance of TStringBuilder. }
  SB := TStringBuilder.Create();
  try
  { Append the message to the string builder. }
  SB.Append('This button was pressed ').
    AppendFormat('%d times', [FClickCount]);
 
  { Show a message; use ToString to access the built string. }
  MessageDlg(SB.ToString(), mtInformation, [mbOK], 0);
 
  { Clear the string builder to allow building another string. }
  SB.Clear();
 
  if SB.Length <> 0 then
    MessageDlg('This cannot happen! Length must be 0 after clear!', mtError, [mbOK], 0);
 
  { Build another string with 2 lines. }
  SB.Append('The name of the button was:').
    AppendLine().
      Append((Sender as TButton).Name);
 
  { Show another message and free the string builder instance. }
  MessageDlg(SB.ToString(), mtInformation, [mbOK], 0);
  finally
  SB.Free;
  end;
end;