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.

2114 questions
92
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,338
  • 13
  • 64
  • 101
90
votes
9 answers

Best practices/performance: mixing StringBuilder.append with String.concat

I'm trying to understand what the best practice is and why for concatenating string literals and variables for different cases. For instance, if I have code like this StringBuilder sb = new StringBuilder("AAAAAAAAAAAAA") …
Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
88
votes
6 answers

Correct way to use StringBuilder in SQL

I just found some sql query build like this in my project: return (new StringBuilder("select id1, " + " id2 " + " from " + " table")).toString(); Does this StringBuilder achieve its aim, i.e reducing memory usage? I doubt that, because in the…
Vaandu
  • 4,857
  • 12
  • 49
  • 75
82
votes
7 answers

StringWriter or StringBuilder

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

How to check if a StringBuilder is empty?

I want to test if the StringBuilder is empty but there is no IsEmpty method or property. How does one determine this?
Boiethios
  • 38,438
  • 19
  • 134
  • 183
76
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
  • 13,216
  • 29
  • 75
  • 108
76
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,211
  • 1
  • 10
  • 21
74
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,692
  • 2
  • 25
  • 40
70
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
  • 10,021
  • 12
  • 41
  • 33
70
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-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
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
  • 90,477
  • 74
  • 177
  • 219
62
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,932
  • 3
  • 45
  • 72
60
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
  • 41,080
  • 29
  • 148
  • 220
58
votes
12 answers

Most efficient solution for reading CLOB to String, and String to CLOB in Java?

I have a big CLOB (more than 32kB) that I want to read to a String, using StringBuilder. How do I do this in the most efficient way? I can not use the "int length" constructor for StringBuilder since the lenght of my CLOB is longer than a "int" and…
Jonas
  • 121,568
  • 97
  • 310
  • 388
57
votes
6 answers

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 ?
Elad Benda
  • 35,076
  • 87
  • 265
  • 471