StringBuilder: how to get the final string?

Someone told me that it is faster to concatenate strings using StringBuilder. I changed my code, but I do not see any properties or methods to get the final assembly string.

How can I get the string?

+45
string stringbuilder c #
Oct 22 '08 at 22:21
source share
6 answers

You can use .ToString() to get String from StringBuilder .

+85
Oct 22 '08 at 22:21
source share

When you say “faster to concatenate a String with a string builder”, this is true only if you re (repeat - re ) concatenating with the same object.

If you just concatenate 2 strings and do something with the result right away as string , there is no point in using StringBuilder .

I just stumbled upon John Skeet, write about it: http://www.yoda.arachsys.com/csharp/stringbuilder.html

If you use StringBuilder , then to get the resulting string it's just a call to ToString() (not surprisingly).

+11
Oct 22 '08 at 22:51
source share

Once you have finished processing with StringBuilder, use the ToString method to return the final result.

From MSDN:

 using System; using System.Text; public sealed class App { static void Main() { // Create a StringBuilder that expects to hold 50 characters. // Initialize the StringBuilder with "ABC". StringBuilder sb = new StringBuilder("ABC", 50); // Append three characters (D, E, and F) to the end of the StringBuilder. sb.Append(new char[] { 'D', 'E', 'F' }); // Append a format string to the end of the StringBuilder. sb.AppendFormat("GHI{0}{1}", 'J', 'k'); // Display the number of characters in the StringBuilder and its string. Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString()); // Insert a string at the beginning of the StringBuilder. sb.Insert(0, "Alphabet: "); // Replace all lowercase k with uppercase K's. sb.Replace('k', 'K'); // Display the number of characters in the StringBuilder and its string. Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString()); } } // This code produces the following output. // // 11 chars: ABCDEFGHIJk // 21 chars: Alphabet: ABCDEFGHIJK 
+8
Oct 22 '08 at 22:21
source share

I would just like to throw it away, which may not necessarily be faster, it will certainly have more memory. This is because the string is immutable in .NET, and every time you change the string, you create a new one.

+3
Oct 22 '08 at 22:24
source share

It is not faster than concat. As smaclell pointed out, the problem is an immutable line, which requires additional highlighting and repetition of existing data.

"a" + "b" + "c" does not work faster with the line builder, but repeated concatenations with an intermediate line become faster and faster as the number of concat is greater:

x = "a"; x + = "b"; x + = "C"; ...

+1
Oct 22 '08 at 22:34
source share

About this fast / best memory:

I reviewed this issue with Java, I believe .NET will be just as smart.

The implementation for String is pretty impressive.

The String object keeps track of “length” and “common” (regardless of the length of the array that contains the string)

So something like

 String a = "abc" + "def" + "ghi"; 

can be implemented (by compiler / runtime) as:

  - Extend the array holding "abc" by 6 additional spaces.
  - Copy def in right after abc 
  - copy ghi in after def. 
  - give a pointer to the "abc" string to a 
  - leave abc length at 3, set a length to 9
  - set the shared flag in both.

Since most lines are short-lived, in many cases this makes some VERY efficient code. The case when it is absolutely NOT effective when you add a line in a loop or when your code looks like this:

 a = "abc"; a = a + "def"; a += "ghi"; 

In this case, you are much better off using the StringBuilder construct.

My point is that you should be careful when optimizing, unless you ALLOW that you know what you are doing, and you are absolutely sure that it is necessary. And you check that the optimized code makes a pass, just copy it in the most readable way and do not try to invent a compiler.

I spent 3 days using strings, caching / reusing string collectors and testing speed, before I looked at the source code of the string and realized that the compiler was already doing it better than I could for my use case. Then I had to explain how I did NOT REALLY know what I was doing, I thought I did ...

+1
Oct 22 '08 at 22:46
source share



All Articles