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
1638
votes
32 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,007
  • 5
  • 18
  • 11
996
votes
18 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
  • 16,626
  • 9
  • 42
  • 59
673
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
  • 51,774
  • 14
  • 88
  • 111
469
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
  • 9,868
  • 7
  • 48
  • 64
252
votes
8 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
  • 14,274
  • 40
  • 148
  • 258
221
votes
11 answers

String, StringBuffer, and StringBuilder

Please tell me a real time situation to compare String, StringBuffer, and StringBuilder?
JavaUser
  • 22,494
  • 44
  • 98
  • 127
208
votes
10 answers

Does JavaScript have a built in stringbuilder class?

I see a few code project solutions. But is there a regular implementation in JavaScript?
leora
  • 163,579
  • 332
  • 834
  • 1,328
161
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
  • 104,787
  • 56
  • 140
  • 177
126
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
  • 12,985
  • 28
  • 96
  • 146
102
votes
14 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,719
  • 9
  • 34
  • 44
98
votes
12 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 trialling comma. What is the best way to remove the trailing comma? Do I have to change data to a string the…
Wesley Skeen
  • 6,937
  • 12
  • 37
  • 55
97
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
  • 1,867
  • 7
  • 24
  • 32
93
votes
8 answers

Newline character in StringBuilder

How do you append a new line(\n\r) character in StringBuilder?
user274364
  • 1,687
  • 1
  • 18
  • 26
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,737
  • 12
  • 46
  • 75
88
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,176
  • 13
  • 72
  • 111
1
2 3
99 100