Questions tagged [stringbuilder]

Stringbuilder is a class that provides a convenient and efficient way of working with text data in Java and .NET framework.

Stringbuilder is a class that provides a convenient and efficient way of working with text data. Implementation of the stringbuilder can be found in .NET framework and Java.

The very idea of the class is being a character array with the capability to insert and append new characters. It avoids copying the strings in the memory, hence improves the performance.

1966 questions
86
votes
9 answers

Why StringBuilder when there is String?

I just encountered StringBuilder for the first time and was surprised since Java already has a very powerful String class that allows appending. Why a second String class? Where can I learn more about StringBuilder?
an00b
  • 11,048
  • 13
  • 60
  • 97
86
votes
12 answers

When to use StringBuilder?

I understand the benefits of StringBuilder. But if I want to concatenate 2 strings, then I assume that it is better (faster) to do it without StringBuilder. Is this correct? At what point (number of strings) does it become better to use…
Shiraz Bhaiji
  • 60,773
  • 31
  • 133
  • 239
84
votes
5 answers

String.Join vs. StringBuilder: which is faster?

In a previous question about formatting a double[][] to CSV format, it was suggested that using StringBuilder would be faster than String.Join. Is this true?
Hosam Aly
  • 38,883
  • 35
  • 132
  • 179
73
votes
7 answers

StringWriter or StringBuilder

What is the difference between StringWriter and StringBuilder and when should I use one or the other?
MAC
72
votes
3 answers

How to use StringBuilder wisely?

I am little confused about using StringBuilder class, first: A string object concatenation operation always creates a new object from the existing string and the new data. A StringBuilder object maintains a buffer to accommodate the concatenation…
Bartłomiej Sobieszek
  • 2,389
  • 2
  • 17
  • 34
71
votes
13 answers

Difference between string and StringBuilder in C#

What is the difference between string and StringBuilder? Also, what would be some examples for understanding?
ratty
  • 12,122
  • 29
  • 69
  • 105
66
votes
5 answers

Why StringJoiner when we already have StringBuilder?

I recently encountered with a Java 8 class StringJoiner which adds the String using the delimiters and adds prefix and suffix to it, but I can't understand the need of this class as it also uses StringBuilder at the backend and also performs very…
Hitesh Garg
  • 1,071
  • 1
  • 9
  • 20
62
votes
15 answers

What is the difference between String and StringBuffer in Java?

What is the difference between String and StringBuffer in Java? Is there a maximum size for String?
Praveen
  • 86,996
  • 72
  • 173
  • 215
62
votes
6 answers

StringBuilder: how to get the final String?

Someone told me that it's faster to concatenate strings with StringBuilder. I have changed my code but I do not see any Properties or Methods to get the final build string. How can I get the string?
Mister Dev
  • 9,031
  • 12
  • 39
  • 33
59
votes
7 answers

When do you use StringBuilder.AppendLine/string.Format vs. StringBuilder.AppendFormat?

A recent question came up about using String.Format(). Part of my answer included a suggestion to use StringBuilder.AppendLine(string.Format(...)). Jon Skeet suggested this was a bad example and proposed using a combination of AppendLine and…
Neil Barnwell
  • 38,622
  • 28
  • 141
  • 213
59
votes
6 answers

Write StringBuilder to Stream

What is the best method of writing a StringBuilder to a System.IO.Stream? I am currently doing: StringBuilder message = new StringBuilder("All your base"); message.Append(" are belong to us"); System.IO.MemoryStream stream = new…
Andy Joiner
  • 5,025
  • 3
  • 39
  • 60
59
votes
5 answers

What is the simplest way to write the contents of a StringBuilder to a text file in .NET 1.1?

I have to use StringBuilder instead of a List of strings because of being stuck with .NET 1.1 for this project. I want to write a series of debug messages I've written to a file to study at my leisure, as there is too much to see on the screen (the…
B. Clay Shannon
  • 1,055
  • 124
  • 399
  • 759
55
votes
3 answers

How to check if a StringBuilder is empty?

I must unit test a method that takes a StringBuilder, two items, and fill the StringBuilder with the discrepancies found between the two items. In my first test, I give to it two identical items, so I want to test if the StringBuilder is…
Boiethios
  • 25,767
  • 8
  • 91
  • 135
52
votes
10 answers

Am I undermining the efficiency of StringBuilder?

I've started using StringBuilder in preference to straight concatenation, but it seems like it's missing a crucial method. So, I implemented it myself, as an extension: public void Append(this StringBuilder stringBuilder, params string[] args) { …
dlras2
  • 8,048
  • 5
  • 46
  • 89
51
votes
5 answers

StringBuffer is obsolete?

In the book "Effective Java", Josh Bloch says that StringBuffer is largely obsolete and should be replaced by the non-synchronized implementation 'StringBuilder' . But in my experience, I've still seen widespread use of the StringBuffer class.…
Varun Achar
  • 13,274
  • 7
  • 53
  • 71