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
30
votes
3 answers

Is StringBuilder.Replace() more efficient than String.Replace?

If you have to use String.Replace() to replace test 50 times, you essentially have to create a new string 50 times. Does StringBuilder.Replace() do this more efficiently? E.g., should I use a StringBuilder if I'm going to be replacing a lot of…
Jim
  • 11,229
  • 20
  • 79
  • 114
30
votes
2 answers

Capitalise first letter in String

I'm having trouble converting the first letter to Capital in a String: rackingSystem.toLowerCase(); // has capitals in every word, so first convert all to lower case StringBuilder rackingSystemSb = new…
Scamparelli
  • 756
  • 1
  • 12
  • 28
29
votes
7 answers

Trim whitespace from the end of a StringBuilder without calling ToString().Trim() and back to a new SB

What is an efficient way to trim whitespace from the end of a StringBuilder without calling ToString().Trim() and back to a new SB new StringBuilder(sb.ToString().Trim()).
Nicholas Petersen
  • 9,104
  • 7
  • 59
  • 69
28
votes
5 answers

StringBuilder and byte conversion

I have the following code: StringBuilder data = new StringBuilder(); for (int i = 0; i < bytes1; i++) { data.Append("a"); } byte[] buffer = Encoding.ASCII.GetBytes(data); But I get this error: cannot convert from 'System.Text.StringBuilder'…
R.Vector
  • 1,669
  • 9
  • 33
  • 41
28
votes
10 answers

How is StringBuffer implementing append function without creating two objects?

It was an interview question. I was asked to implement the StringBuffer append function. I saw the code after the interview. But I cannot understand how the operation is done with creation of a single object. I am thinking like this. String s =…
javaMan
  • 6,352
  • 20
  • 67
  • 94
28
votes
4 answers

When was Javac StringBuilder/StringBuffer optimization introduced?

I know that Javac compiler is able to transform String concatenation + using StringBuilder/StringBuffer, and I'm curious to know starting from which version this change was introduced? I'm using this sample code: public class Main { public static…
Nicolas C
  • 1,584
  • 2
  • 17
  • 33
28
votes
2 answers

What is the use of the return value of the StringBuilder Append(string...) function?

The complete syntax of StringBuilder's Append(string s) function (and similar functions) is StringBuilder myStringBuilder.Append(string myString) since myStringBuilder.Append(string myString) already adds the string to myStringBuilder, I was…
sth_Weird
  • 620
  • 8
  • 16
28
votes
4 answers

Regex replacements inside a StringBuilder

I'm writing the contents of a text file to a StringBuilder and I then want to perform a number of find/replace actions on the text contained in the StringBuilder using regular expressions. I've run into a problem as the StringBuilder replace…
ipr101
  • 24,096
  • 8
  • 59
  • 61
28
votes
2 answers

.NET StringBuilder preappend a line

I know that the System.Text.StringBuilder in .NET has an AppendLine() method, however, I need to pre-append a line to the beginning of a StringBuilder. I know that you can use Insert() to append a string, but I can't seem to do that with a line, is…
Art F
  • 3,992
  • 10
  • 49
  • 81
28
votes
2 answers

String builder vs string concatenation

What is the benefit and trade-off of using a string builder over pure string concatenation? new StringBuilder(32).append(str1) .append(" test: ") .append(val) .append(" is changed") …
Chun ping Wang
  • 3,879
  • 12
  • 42
  • 53
26
votes
1 answer

StringBuilder Class OutOfMemoryException

I have written following function public void TestSB() { string str = "The quick brown fox jumps over the lazy dog."; StringBuilder sb = new StringBuilder(); int j = 0; int len = 0; try { for (int i = 0; i < (10000000 * 2); i++) …
Atur
  • 1,712
  • 6
  • 32
  • 42
26
votes
3 answers

Generate a String that matches a RegEx in Python

Possible Duplicate: Reversing a regular expression in python I think I ran into a problem that sounds easier than it is... I'm not too sure. I want to define a regular expression, and I want to build a number of strings matching it. Is there any…
wishi
  • 7,188
  • 17
  • 64
  • 103
26
votes
5 answers

Java StringBuilder(StringBuffer)'s ensureCapacity(): Why is it doubled and incremented by 2?

I have searched about this, but I couldn't find why StringBuilder's ensureCapacity() method won't lengthen the old capacity by just doubling but also adding two. So, when default capacity of 16 is full, next lengthened value will be 34 unless whole…
Philip Paek
  • 271
  • 2
  • 6
26
votes
8 answers

StringBuilder - Reset or create a new

I have a condition that a StringBuilder keeps storing lines matching a pattern from a large flat file (100's of MB). However after reaching a condition I write the content of the StringBuilder varialble to a textfile. Now I wonder if I should use…
Chandru
  • 336
  • 1
  • 5
  • 14
26
votes
5 answers

Is there any scenario where the Rope data structure is more efficient than a string builder

Related to this question, based on a comment of user Eric Lippert. Is there any scenario where the Rope data structure is more efficient than a string builder? It is some people's opinion that rope data structures are almost never better in…
luvieere
  • 37,065
  • 18
  • 127
  • 179