tags:

views:

1053

answers:

5

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).

+15  A: 

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;

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.

ulrichb
Could you please move the StringBuilder creation before the try (in the interest of promoting good habits)?
mghie
+4  A: 

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).

gabr
+1  A: 

You might find this series of blog entries interesting.

Ulrich Gerhardt
+1  A: 

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/

Herbert Sitz
That assertion in [1] is of course BS, as one can see easily from the timing results in [4]. Whether ones own program does use string building in a way that the difference matters is a completely different question, obviously.
mghie
Yes, I agree assertion in [1] may be wrong. But I don't think I'd jump to that just from a single benchmark that iteratively concatenates a single space to a base string 10,000,000 times. Who knows whether the observed speed differences translate to particular real world concatenation scenarios?
Herbert Sitz
@Herbert: This benchmark is already chosen to have the minimum difference, because nothing is done between the concatenations that would allocate memory. In real world scenarios in-place memory reallocation would often be impossible, so TStringBuilder would come up much much better.
mghie
+2  A: 

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.

skamradt
This may be right if all you ever do is work with a few ten characters in a string. But for creating any complex long string with memory access patterns that prevent it from being reallocated in-place this is not true. Also, your Delphi timeline seems a little off - decades ??? 13 years since D2...
mghie
@mghie Was including Pascal, which I have been programming in since the late 80's.
skamradt
mghie: any examples? Afaik native stringbuilder only preallocates a bit more extra memory than normal (normal strings only have extra capacity due to the heap allocation granularity). I wonder if tstringbuilder is faster at all until your strings get longer than thousands of chars. I can find preciously little benchmark info on this topic.
Marco van de Voort