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
18
votes
7 answers

No reverse method in String class in Java?

Why there is no reverse method in String class in Java? Instead, the reverse() method is provided in StringBuilder? Is there a reason for this? But String has split(), regionMatches(), etc., which are more complex than the reverse() method. When…
Pradeep Kumar
  • 471
  • 2
  • 6
  • 14
18
votes
7 answers

At what point does using a StringBuilder become insignificant or an overhead?

Recently I have found myself using StringBuilder for all string concatenations, big and small, however in a recent performance test I swapped out a colleague's stringOut = string1 + "." string2 style concatenation (being used in a 10000x + loop with…
johnc
  • 39,385
  • 37
  • 101
  • 139
18
votes
1 answer

StringBuilder.toString() is printed as empty string in Eclipse-console when too big?

The following returns nothing for me in eclipse, is this expected behaviour ? StringBuilder sb = new StringBuilder(""); for(int i = 0; i < 256*256*2*6; i++) { sb.append("a"); } System.out.println(sb.toString()); The code returns without error…
HopefullyHelpful
  • 1,652
  • 3
  • 21
  • 37
18
votes
8 answers

StringBuilder capacity()

I noticed that the capacity method returns StringBuilder capacity without a logic way ... sometime its value is equals to the string length other time it's greater... is there an equation for know which is its logic?
xdevel2000
  • 20,780
  • 41
  • 129
  • 196
17
votes
2 answers

How to clone() StringBuilder

Is there some ways how I could clone StringBuilder ? I am reading files by bits then convert these bits to ASCII chars after that I collect chars into String builder and when I have for example 8 chars I put that String Builder object into Array…
Streetboy
  • 4,351
  • 12
  • 56
  • 101
17
votes
2 answers

Android StringBuilder vs String Concatenation

I was reading this documentation page, http://developer.android.com/reference/android/util/Log.html. The section here caught my eye: Tip: Don't forget that when you make a call like Log.v(TAG, "index=" + i); that when you're building the string to…
Jeremy Edwards
  • 14,620
  • 17
  • 74
  • 99
17
votes
4 answers

Stream StringBuilder to file

I need to create a large text document. I currently use StringBuilder to make the document and then call File.WriteallText(filename,sb.ToString). Unfortunately, this is now starting to throw out of memory exceptions. Is there a better way to…
GreyCloud
  • 3,030
  • 5
  • 32
  • 47
17
votes
6 answers

Is there any benefit to returning the result of assigning a value to a local variable rather than the value directly?

I am doing a java code inspection. Here is a function (snippet): String getValue() { String res; StringBuilder strBuilder = new StringBuilder(); // More code here that sets strBuilder return res =…
user3257891
  • 275
  • 1
  • 4
  • 13
17
votes
8 answers

How to Reassign value of StringBuffer?

How can we re assign the value of a StringBuffer or StringBuilder Variable? StringBuffer sb=new StringBuffer("teststr"); Now i have to change the value of sb to "testString" without emptying the contents. I am looking at a method which can do this…
Biju CD
  • 4,999
  • 11
  • 34
  • 55
17
votes
5 answers

StringBuilder.ToString() throws OutOfMemoryException

I have a created a StringBuilder of length "132370292", when I try to get the string using the ToString() method it throws OutOfMemoryException. StringBuilder SB = new StringBuilder(); for(int i =0; i<=5000; i++) { SB.Append("Some Junk Data for…
Thiru
  • 3,293
  • 7
  • 35
  • 52
17
votes
4 answers

deleteCharAt or setLength, which way is better to remove last char from a StringBuilder/StringBuffer

In many cases, we need to delete the last char of a StringBuilder/StringBuffer. For example, given a int[]{1,2,3}, to implement a String toString(int[] a) method, contacting each elements with a comma separator. The output should be 1,2,3, no…
Weibo Li
  • 3,565
  • 3
  • 24
  • 36
17
votes
5 answers

.NET StringBuilder - check if ends with string

What is the best (shortest and fastest) way to check if StringBuilder ends with specific string? If I want to check just one char, that's not a problem sb[sb.Length-1] == 'c', but how to check if it's ends with longer string? I can think about…
Alex Dn
  • 5,465
  • 7
  • 41
  • 79
17
votes
4 answers

replaceAll for StringBuilder with regex support?

I've looked in the Java API and some common 3rd party libraries but I'm unable to find a suitable method that will do what String.replaceAll does, except for StringBuilder. I know that with a little work, it can be done for StringBuffer, but I don't…
Jin Kim
  • 16,562
  • 18
  • 60
  • 86
17
votes
3 answers

Where to use StringBuffer/StringBuilder than String

Possible Duplicate: String, StringBuffer, and StringBuilder We know that String are immutable where StringBuffer/StringBuilder are mutable. But sometimes we get confused what to use in our code.. the String or StringBuffer/StringBuilder ??…
Soumyadip Das
  • 1,781
  • 3
  • 16
  • 34
16
votes
5 answers

Read all ini file values with GetPrivateProfileString

I need a way to read all sections/keys of ini file in a StringBuilder variable: [DllImport("kernel32.dll")] private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int…
leon22
  • 5,280
  • 19
  • 62
  • 100