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
51
votes
5 answers

StringBuilder for PHP

Has someone made a StringBuilder implementation in PHP?
Mark
  • 57,724
  • 41
  • 114
  • 149
51
votes
6 answers

How the StringBuilder class is implemented? Does it internally create new string objects each time we append?

How the StringBuilder class is implemented? Does it internally create new string objects each time we append?
Ram
  • 10,646
  • 15
  • 57
  • 90
50
votes
4 answers

How does StringBuilder work internally in C#?

How does StringBuilder work? What does it do internally? Does it use unsafe code? And why is it so fast (compared to the + operator)?
Alon Gubkin
  • 53,054
  • 52
  • 181
  • 282
49
votes
6 answers

How to append two stringBuilders?

Is there a way to append two string builders? And if so - does it perform better than appending a string to a StringBuilder ?
Elad Benda
  • 30,720
  • 75
  • 234
  • 415
49
votes
12 answers

Most efficient solution for reading CLOB to String, and String to CLOB in Java?

I have a big CLOB (more than 32kB) that I want to read to a String, using StringBuilder. How do I do this in the most efficient way? I can not use the "int length" constructor for StringBuilder since the lenght of my CLOB is longer than a "int" and…
Jonas
  • 97,987
  • 90
  • 271
  • 355
47
votes
4 answers

Java: String concat vs StringBuilder - optimised, so what should I do?

In this answer, it says (implies) that String concatenation is optimised into StringBuilder operations anyway, so when I write my code, is there any reason to write StringBuilder code in the source? Note that my use case is different from the OP's…
Soyuz
  • 1,027
  • 1
  • 8
  • 16
46
votes
3 answers

How do I prove programmatically that StringBuilder is not threadsafe?

How do I prove programmatically that StringBuilder is not threadsafe? I tried this, but it is not working: public class Threadsafe { public static void main(String[] args) throws InterruptedException { long startdate =…
Aamir Suhail
  • 485
  • 4
  • 3
42
votes
9 answers

Dumping a Java StringBuilder to File

What is the most efficient/elegant way to dump a StringBuilder to a text file? You can do: outputStream.write(stringBuilder.toString().getBytes()); But is this efficient for a very long file? Is there a better way?
Patrick
  • 1,254
  • 2
  • 12
  • 21
41
votes
6 answers

Why use TagBuilder instead of StringBuilder?

what's the difference in using tag builder and string builder to create a table in a htmlhelper class, or using the HtmlTable? aren't they generating the same thing??
DaveDev
  • 38,095
  • 68
  • 199
  • 359
40
votes
2 answers

Why use StringBuilder explicitly if the compiler converts string concatenation to a StringBuilder automatically?

Possible Duplicate: StringBuilder vs String concatenation in toString() in Java I am wondering, since the compiler internally uses a StringBuilder to append Strings when performing String concatenation, then what's the point and why should I…
peter
  • 7,752
  • 16
  • 64
  • 89
38
votes
9 answers

StringBuilder.Append Vs StringBuilder.AppendFormat

I was wondering about StringBuilder and I've got a question that I was hoping the community would be able to explain. Let's just forget about code readability, which of these is faster and why? StringBuilder.Append: StringBuilder sb = new…
Sergio
  • 7,761
  • 10
  • 44
  • 76
37
votes
5 answers

Equivalent of StringBuilder for byte arrays

This is a simple one, and one that I thought would have been answered. I did try to find an answer on here, but didn't come up with anything - so apologies if there is something I have missed. Anyway, is there an equivalent of StringBuilder but for…
Matt Whitfield
  • 6,158
  • 3
  • 26
  • 42
37
votes
9 answers

StringBuilder append() and null values

I have a list of Strings, and I want to concatenate them with spaces in between. So I'm using StringBuilder. Now if any of the Strings are null, they get stored in the StringBuilder literally as 'null'. Here is a small program to illustrate the…
look4chirag
  • 383
  • 1
  • 3
  • 6
37
votes
6 answers

Why does StringBuilder.AppendLine not add a new line with some strings?

I'm trying to use stringbuilder to create a body of string to be used in a text (not HTML) email. However some lines (where i include dynamic data, a new line is not added, but in some the newline works as intended. Is there something basic i'm…
kolin
  • 2,185
  • 1
  • 23
  • 43
36
votes
11 answers

How to remove empty lines from a formatted string

How can I remove empty lines in a string in C#? I am generating some text files in C# (Windows Forms) and for some reason there are some empty lines. How can I remove them after the string is generated (using StringBuilder and TextWrite). Example…
Saeid Yazdani
  • 12,365
  • 48
  • 158
  • 270