Delphi StringBuilder

Yes, Delphi offers TStringBuilder (since version 2009):

procedure TestStringBuilder;
var
  I: Integer;
  StringBuilder: TStringBuilder;
begin
  StringBuilder := TStringBuilder.Create;
  try
    for I := 1 to 10 do
    begin
      StringBuilder.Append('a string ');
      StringBuilder.Append(66); //add an integer
      StringBuilder.Append(sLineBreak); //add new line
    end;

    OutputWriteLine('Final string builder length: ' +
                    IntToStr(StringBuilder.Length));
  finally
    StringBuilder.Free;
  end;
end;

And yes, you are right. s := s + 'text'; isn't really slower than using TStringBuilder.


In older Delphis, you can use Hallvard Vassbotn's HVStringBuilder. I failed to find the sources on his blog, but you can fetch them in the OmniThreadLibrary source tree, for example (you'll need files HVStringBuilder.pas and HVStringData.pas).

Tags:

Delphi