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
1732
votes
33 answers

Difference between StringBuilder and StringBuffer

What is the main difference between StringBuffer and StringBuilder? Is there any performance issues when deciding on any one of these?
blacktiger
  • 18,957
  • 5
  • 20
  • 11
1104
votes
20 answers

StringBuilder vs String concatenation in toString() in Java

Given the 2 toString() implementations below, which one is preferred: public String toString(){ return "{a:"+ a + ", b:" + b + ", c: " + c +"}"; } or public String toString(){ StringBuilder sb = new StringBuilder(100); return…
non sequitor
  • 18,296
  • 9
  • 45
  • 64
747
votes
9 answers

How can I clear or empty a StringBuilder?

I'm using a StringBuilder in a loop and every x iterations I want to empty it and start with an empty StringBuilder, but I can't see any method similar to the .NET StringBuilder.Clear in the documentation, just the delete method which seems overly…
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
527
votes
17 answers

Remove last character of a StringBuilder?

When you have to loop through a collection and make a string of each data separated by a delimiter, you always end up with an extra delimiter at the end, e.g. for (String serverId : serverIds) { sb.append(serverId); sb.append(","); } Gives…
Matthew
  • 10,988
  • 11
  • 54
  • 69
289
votes
7 answers

How to append a newline to StringBuilder

I have a StringBuilder object, StringBuilder result = new StringBuilder(); result.append(someChar); Now I want to append a newline character to the StringBuilder. How can I do it? result.append("/n"); Does not work. So, I was thinking about…
Illep
  • 16,375
  • 46
  • 171
  • 302
261
votes
11 answers

Does JavaScript have a built-in stringbuilder class?

I see a few The Code Project solutions. But is there a regular implementation in JavaScript?
leora
  • 188,729
  • 360
  • 878
  • 1,366
224
votes
12 answers

String, StringBuffer, and StringBuilder

Please tell me a real time situation to compare String, StringBuffer, and StringBuilder?
JavaUser
  • 25,542
  • 46
  • 113
  • 139
164
votes
12 answers

Is String.Format as efficient as StringBuilder

Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new StringBuilder(); string cat = "cat"; sb.Append("the ").Append(cat).(" in the hat"); string s = sb.ToString(); would that be as efficient or any more efficient as…
lomaxx
  • 113,627
  • 57
  • 144
  • 179
159
votes
12 answers

How can we prepend strings with StringBuilder?

I know we can append strings using StringBuilder. Is there a way we can prepend strings (i.e. add strings in front of a string) using StringBuilder so we can keep the performance benefits that StringBuilder offers?
burnt1ce
  • 14,387
  • 33
  • 102
  • 162
122
votes
13 answers

Best way to remove the last character from a string built with stringbuilder

I have the following data.AppendFormat("{0},",dataToAppend); The problem with this is that I am using it in a loop and there will be a trailing comma. What is the best way to remove the trailing comma? Do I have to change data to a string and then…
Wesley Skeen
  • 7,977
  • 13
  • 42
  • 56
116
votes
9 answers

java: use StringBuilder to insert at the beginning

I could only do this with String, for example: String str = ""; for (int i = 0; i < 100; i++) { str = i + str; } Is there a way to achieve this with StringBuilder? Thanks.
user685275
  • 2,097
  • 8
  • 26
  • 32
110
votes
15 answers

Is it better to reuse a StringBuilder in a loop?

I've a performance related question regarding use of StringBuilder. In a very long loop I'm manipulating a StringBuilder and passing it to another method like this: for (loop condition) { StringBuilder sb = new StringBuilder(); …
Pier Luigi
  • 7,871
  • 9
  • 36
  • 46
103
votes
8 answers

Newline character in StringBuilder

How do you append a new line(\n\r) character in StringBuilder?
user274364
  • 1,797
  • 2
  • 20
  • 27
98
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
  • 41,555
  • 36
  • 141
  • 182
95
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
  • 64,065
  • 34
  • 143
  • 252
1
2 3
99 100