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
16
votes
8 answers

Why use StringBuilder? StringBuffer can work with multiple thread as well as one thread?

Suppose our application have only one thread. and we are using StringBuffer then what is the problem? I mean if StringBuffer can handle multiple threads through synchronization, what is the problem to work with single thread? Why use StringBuilder…
16
votes
8 answers

StringBuilder for string concatenation throws OutOfMemoryException

We mostly tend to following the above best practice. Have a look at String vs StringBuilder But StringBuilder could throw OutOfMemoryException even when there is sufficient memory available. It throws OOM exception because it needs "continuous…
rajesh pillai
  • 8,102
  • 7
  • 43
  • 60
16
votes
5 answers

What does " StringBuilders are not thread-safe" mean?

I have read some articles about the pros and cons of String and StringBuilder in the Java Programming language. In one of the articles, the author mentioned that: StringBuilder is not Thread-safe, so in multiple threads use …
Elyas Hadizadeh
  • 3,289
  • 8
  • 40
  • 54
16
votes
2 answers

Does Javac's StringBuilder optimisation do more harm than good?

Let's say we have some code such as the following: public static void main(String[] args) { String s = ""; for(int i=0 ; i<10000 ; i++) { s += "really "; } s += "long string."; } (Yes, I know a far better implementation…
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
15
votes
1 answer

Why does Java StringBuilder have a constructor for CharSequence and another one for String?

Knowing that String implements CharSequence interface, so why does StringBuilder have a constructor for CharSequence and another one for String? No indication in the javadoc ! public final class String implements java.io.Serializable,…
AbdelRahmane
  • 289
  • 2
  • 12
15
votes
9 answers

How to trim a java stringbuilder?

I have a StringBuilder object that needs to be trimmed (i.e. all whitespace chars /u0020 and below removed from either end). I can't seem to find a method in string builder that would do this. Here's what I'm doing now: String trimmedStr =…
CodeFusionMobile
  • 14,812
  • 25
  • 102
  • 140
15
votes
5 answers

Does StringBuilder become immutable after a call to ToString?

I distinctly remember from the early days of .NET that calling ToString on a StringBuilder used to provide the new string object (to be returned) with the internal char buffer used by StringBuilder. This way if you constructed a huge string using…
M.S
  • 1,580
  • 1
  • 13
  • 22
15
votes
5 answers

How to put spaces in a stringbuilder

Hello I have following method to display a promotion line when I comment a shoutbox: public String getShoutboxUnderline(){ StringBuilder builder = new StringBuilder(); builder.append("watch"); builder.append("on"); …
KeinHappyAuer
  • 173
  • 1
  • 1
  • 9
15
votes
2 answers

Java for loop isn't terminating in my code

For some reason my for loop is not terminating in my CapitalizeFirstSentence method. I set a breakpoint at that line and the condition (i != -1) is unmet, so the loop should terminate, but it doesn't! It works when I use (i > 0) for the…
Tommy Saechao
  • 1,099
  • 4
  • 17
  • 28
15
votes
6 answers

When and Why Should I Use TStringBuilder?

I converted my program from Delphi 4 to Delphi 2009 a year ago, mainly to make the jump to Unicode, but also to gain the benefits of all those years of Delphi improvements. My code, of course, is therefore all legacy code. It uses short strings that…
lkessler
  • 19,819
  • 36
  • 132
  • 203
15
votes
2 answers

Fastest search method in StringBuilder

I have a StringBuilder named stb_Swap_Tabu used to store Course's Names, I am using the following method to find a course: stb_Swap_Tabu.ToString.Contains("CourseName") in my case, Performance is the most important issue. Is there any faster way?
Alaa Alweish
  • 8,904
  • 16
  • 57
  • 84
14
votes
5 answers

Java StringBuilder and Thread Safety

I am building up a String out of multiple pieces and want to use either StringBuffer or StringBuilder to do so. From the Java 5 docs, I see that StringBuilder is preferred when possible, with the caveat that Instances of StringBuilder are not safe…
Ben
  • 221
  • 1
  • 3
  • 5
14
votes
1 answer

In C#, best way to check if stringbuilder contains a substring

I have an existing StringBuilder object, the code appends some values and a delimiter to it. I want to modify the code to add the logic that before appending the text, it will check if it already exists in the StringBuilder. If it does not, only…
Sri Reddy
  • 6,832
  • 20
  • 70
  • 112
14
votes
4 answers

Java: Remove String from StringBuilder

I want to remove String from StringBuilder Example String aaa = "sample"; String bbb = "sample2"; String ccc = "sample3"; In another part StringBuilder ddd = new StringBuilder(); ddd.append(aaa); ddd.append(bbb); ddd.append(ccc); I want to check…
user2341387
  • 303
  • 1
  • 5
  • 14
14
votes
3 answers

Interesting OutOfMemoryException with StringBuilder

I have the need to continuously build large strings in a loop and save them to database which currently occasionally yields an OutOfMemoryException. What is basically going on here is I create a string using XmlWriter with StringBuilder based on…
bitbonk
  • 48,890
  • 37
  • 186
  • 278