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, while loops).
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).
The TStringBuilder mentioned is the way to go. In your specific case concatenation may be fine, but I'd always try the alternative anyway.
I am creating an EPUB body content xhtml file in memory (Delphi XE) and it was taking so long to produce it that I never once let it finish (about 5 minutes plus before abandoning). This is a real life example combining around 800,000 characters of text. Taking the EXACT same code and directly replacing the s:=s+'' style statements with TStringBuilder.Append statements reduced it to around 3 seconds. To reiterate, there were no logic changes beyond a switch away from concatenation.
Delphi does not "REQUIRE" a string builder class, but one is provided for Delphi 2009 if you so desire to use it. Your example of s := s + 'some string'; is a typical method of concatinating strings and has been used in Pascal/Delphi for the past few decades without any significant problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With