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).
views:
1053answers:
5Yes, 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;
Further information (benchmark):
http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/.
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).
I've listed some good resources on Delphi strings below.
As someone else said, simple concatenation using the '+' operator with the general purpose string types is about as fast as using TStringbuilder (at least for operations of the form: 's := s + [. . . ]'). Don't know if it's true or not, but performance is at least close enough that [1], below, asserts that "Strings concatenation in Delphi is so fast that the new optimized StringBuilder class in Delphi 2009 cannot beat it." This is because the strings are modified in place and Delphi transparenty allocates more memory for the base string if needed, rather than doing a copy-on-write operation of all the data to a new location in memory.
[1] http://blog.marcocantu.com/blog/delphi_super_duper_strings.html
[2] http://conferences.codegear.com/he/article/32120
[3] http://www.codexterity.com/delphistrings.htm
[4] http://www.monien.net/blog/index.php/2008/10/delphi-2009-tstringbuilder/
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.