Elad Benda
Elad Benda

Reputation: 36654

How to append two stringBuilders?

Is there a way to append two string builders? And if so - does it perform better than appending a string to a StringBuilder ?

Upvotes: 60

Views: 58152

Answers (6)

Nigrimmist
Nigrimmist

Reputation: 12378

You don't need to call .ToString(). You should simply append one to another. That's all. It will be better against direct .ToString() call for next reason :

1) StringBuilder does not have constructor with StringBuilder as a param, string, int/string etc. StringBuilder overriding .ToString() and as a result :

StringBuilder sb1 = new StringBuilder("1");
sb1.Append(new StringBuilder("2"));
Console.WriteLine(sb1);

that code will call overrided version of .ToString() automatically. Output will be "12";

2)If StringBuilder will be added as incoming param to StringBuilder constructor's in next framework versions, your code will be clear and ready for correct appending without any refactoring.

Have a good day!

Upvotes: 15

Gaurav Agrawal
Gaurav Agrawal

Reputation: 4431

Just like that....

StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
sb.Append(sb1.ToString());

Upvotes: 25

Rolf Kristensen
Rolf Kristensen

Reputation: 19867

If the StringBuilder is large, then this will minimize string-allocations (especially if you can provide a reusable char-buffer):

    public static void CopyTo(this StringBuilder source, StringBuilder dest)
    {
        char[] buffer = new char[Math.Min(source.Length, 1024)];
        CopyTo(source, dest, buffer);
    }

    public static void CopyTo(this StringBuilder source, StringBuilder dest, char[] buffer)
    {
        dest.EnsureCapacity(dest.Length + source.Length);
        for (int i = 0; i < source.Length; i += buffer.Length)
        {
            int charCount = Math.Min(source.Length - i, buffer.Length);
            source.CopyTo(i, buffer, 0, charCount);
            dest.Append(buffer, 0, charCount);
        }
    }

Upvotes: 6

Mark Hurd
Mark Hurd

Reputation: 10931

I know this is three years later, but the .NET 4 StringBuilder behaves differently anyway.

Nevertheless, it does still come back to "what do you want to do?" Are you looking for simply the most performant way of appending two StringBuilders and continuing on with just the latter result? Or are you expecting to continue working with the existing buffered value of the appended StringBuilder?

For the former, and always in .NET 4,

frontStringBuilder.Append(backStringBuilder);

is best.

For the latter scenario in .NET 2/3.5,

frontStringBuilder.Append(backStringBuilder.ToString(0, backStringBuilder.Length));

is best (and won't hurt performance in .NET 4).

Upvotes: 43

Ondrej Petrzilka
Ondrej Petrzilka

Reputation: 1569

This will do it without allocations

StringBuilder sb = new StringBuilder("aaaa");    
StringBuilder second = new StringBuilder("bbbbb");
sb.EnsureCapacity(sb.Length + second.Length);
for (int i = 0; i < second.Length; i++)
{
    sb.Append(second[i]);
}

Upvotes: 19

Dummy01
Dummy01

Reputation: 1995

Simply as that:

firstStringBuilder.Append(secondStringBuilder.ToString());

Upvotes: 4

Related Questions