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

- 563
- 1
- 4
- 8
-
1You might find [this series of blog entries](http://www.deltics.co.nz/blog/?p=375) interesting. – Uli Gerhardt Mar 26 '09 at 18:13
-
1You don't want to use TStringBuilder. It is very slow. – Gabriel Nov 13 '15 at 08:36
7 Answers
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.

- 8,799
- 13
- 70
- 144

- 19,610
- 8
- 73
- 87
-
6StringBuilder is not that efficient actually: http://www.delphitools.info/2013/10/30/efficient-string-building-in-delphi/ – Eric Grange Nov 01 '13 at 04:53
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).

- 26,580
- 9
- 75
- 141
-
HVStringBuilder for olds Delphi app is a good thing to have ! Thanks for this answer. – Jean.R Jul 03 '13 at 11:48
-
Before grabbing the source code for HVStringBuilder, can you tell us if this library is faster than s:= s+ x ? Many thanks. – Gabriel Mar 18 '16 at 08:15
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.

- 15,366
- 2
- 36
- 53
-
1This 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 Mar 31 '09 at 03:56
-
@mghie Was including Pascal, which I have been programming in since the late 80's. – skamradt Apr 22 '09 at 17:51
-
2mghie: 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 Jun 01 '09 at 14:49
-
It seems not faster at all http://www.delphitools.info/2013/10/30/efficient-string-building-in-delphi/2/ – user2091150 Jun 17 '14 at 08:59
I'm really surprised that no one mentioned in any of the comments or examples that you can instruct TStringBuilder to preallocate a buffer appropriate to the task during instantiation. In other words, if you can come up with a simple estimate of how much memory you'll probably need, pad that a bit, and use that value to instantiate TStringBuilder , you avoid the memory reallocations that slow simple string concatenation to a crawl:
buff := TStringBuilder.Create( tmpEstimatedSize );
I use TStringBuilder regularly in new code and to optimize old code, and the CPU savings when building large strings incrementally is dramatic. Now to be transparent, if I just have a handful of strings to concatenate, I don't bother with TStringBuilder. But if I'm, say, serializing what could potentially be a large amount of data, TStringBuilder is the obvious solution.

- 764
- 6
- 7
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.

- 51
- 1
- 1
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/

- 21,858
- 9
- 50
- 54
-
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 Mar 27 '09 at 05:03
-
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 Mar 30 '09 at 00:47
-
@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 Mar 31 '09 at 03:50
-
2@mghien FastMM does speculative reallocation. In practice TStringBuilder is not the best choice, and if you go multi-threaded, it's actually very poor as it'll use only one CPU due to implicit bus locks and critical sections. – Eric Grange Nov 01 '13 at 04:57
s := s + 'some string' could be terribly slow if you do it in a loop because of the memory allocation involved. I have dome some tests that show that preallocating memory could be 132 times (YES YOU READ IT RIGHT) faster!!!!
The code is like this:
marker:= 1;
CurBuffLen:= 0;
for i:= 1 to Length(FileBody) DO
begin
if i > CurBuffLen then
begin
SetLength(s, CurBuffLen+ BuffSize);
CurBuffLen:= Length(s)
end;
s[marker]:= FileBody[i];
Inc(marker);
end;
See my answer here for details: When and Why Should I Use TStringBuilder?
Note: my code is optimized for
s:= s+ c
where c is a char, but you can easily adapt it
for s:= s + 'some string'
.

- 20,797
- 27
- 159
- 293