Where are the strings more useful than StringBuilder?

Many questions have already been asked about the differences between a string and a string builder, and most people think that a string builder is faster than a string. I'm curious to know if the string builder is too good, so why do I need a string? Moreover, can some body give me an example where a line would be more useful than a line builder?

+5
string stringbuilder c # heuristics
Jun 18 '10 at
source share
9 answers

This is not a case that is more useful ...

A String - String - one or more characters next to each other. If you want to change the string in some way, it will simply create more lines, because they are immutable .

A StringBuilder is a class that creates strings. It provides the means to build them without creating a large number of repeated lines in memory. The end result will always be String .

Do not do this

 string s = "my string"; s += " is now a little longer"; 

or

 s = s + " is now longer again"; 

This would create 5 lines in memory (in effect, see below).

Do it:

 StringBuilder sb = new StringBuilder(); sb.Append("my string"); sb.Append(" is now a little longer"); sb.Append(" is now longer again"); string s = sb.ToString(); 

This would create line 1 in memory (see below).

You can do it:

 string s = "It is now " + DateTime.Now + "."; 

This creates only 1 line in memory.

As a side note, creating a StringBuilder anyway requires a certain amount of memory. As a rough rule of thumb:

  • Always use StringBuilder if you are concatenating strings in a loop.
  • Use StringBuilder if you are concatenating a string more than 4 times.
+3
Jun 18 2018-10-18
source share

StringBuilder, as the name implies, is used to construct strings, while strings are immutable character values. Use strings when you want to transfer personal data, use StringBuilder if you want to manipulate character data.

+11
Jun 18 '10 at 13:03
source share

StringBuilder is the best option when building and modifying strings using heavy loops or iterations.

Simple string operations, such as multiple concatenations or just storing a string value, are much better for a standard string object.

A similar question? String vs. StringBuilder

+7
Jun 18 '10 at 13:02
source share

StringBuilder is better when you need to change the value. Otherwise, there is no need for this.

If I need to use the same string value in 10 places like this, I will use String. If I need to change it 10 times, StringBuilder.

+2
Jun 18 '10 at 13:01
source share

Are you really using line builder here?

 Console.WriteLine("MyString"); 

StringBuilder works great when you need to do large amounts of concatenation to create a string.

Jon Skeet has a great article: http://www.yoda.arachsys.com/csharp/stringbuilder.html

Check out the section that begins β€œSo I have to use StringBuilder everywhere, right?”

He's writing:

The important difference between this is the example, and the previous one, we can easily imagine all the lines that need to be combined together in one call to String.Concat. Which means that no intermediate lines are needed. StringBuilder is effective in the first example, because it acts like a container for an intermediate result without having to copy this result every time - when there is no intermediate result in any case, it does not have an advantage.




+1
Jun 18 '10 at 12:59
source share

StringBuilder is no faster than using a string because it just creates a string.

StringBuilder can be faster when you need to combine multiple lines (e.g. in a loop). You have to pay a performance penalty if you need to convert the contents of a string stanza to a string.

One of the real advantages of StringBuilder over a string is that you can pass it in several ways and add strings along the way. You cannot do this by passing a string object. Therefore, if you have a recursive operation when you are building a JSON object or something like that, you can pass a StringBuilder and add values.

0
Jun 18 '10 at 13:03
source share

A string is good because it is immutable. And this immutable is good, this is what functional languages ​​prove.

Because if you have a link (link) to the string "This is a string", you can be sure that it will have the same value forever. Although the value of StringBuilder can be changed (by calling a method, another thread, etc.).

But sometimes you need pure speed and optimized memory usage. In this case, you are using a string constructor.

Example:

  var a = "This is a string"; var b = a; a += ". And some part is added"; // a now references another string in memory var aSB = new StringBuilder("This is a string"); var bSB = aSB aSB.Append(". And some part is added"); // aSB and bSB still reference the same memory spot Console.WriteLine(b); // ==> This is a string Console.WriteLine(bSB); // ==> This is a string. And some part is added 
0
Jun 18 '10 at 13:04 on
source share

In short, this is a compromise between volatile and unchanging

See this: StringBuilder vs String

0
Jun 18 '10 at 13:06 on
source share

The analysis to be used is provided at: Effective String Merge .

Essentially (though read the link for a complete analysis), use StringBuilder when concatenating in a non-trivial loop.

0
Jun 18 '10 at 2:58 p.m.
source share



All Articles