Delphi StringBuilder

后端 未结 7 1711
别那么骄傲
别那么骄傲 2021-02-12 12:26

Exists in Delphi something like the Java or C# StringBuilder? Or Delphi does not need StringBuilder and s := s + \'some string\'; is good expression (mainly in for,

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-12 13:16

    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.

提交回复
热议问题