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
54
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
  • 56,458
  • 54
  • 195
  • 288
53
votes
5 answers

StringBuffer is obsolete?

In the book "Effective Java", Josh Bloch says that StringBuffer is largely obsolete and should be replaced by the non-synchronized implementation 'StringBuilder' . But in my experience, I've still seen widespread use of the StringBuffer class.…
Varun Achar
  • 14,781
  • 7
  • 57
  • 74
53
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
  • 11,404
  • 15
  • 62
  • 93
52
votes
5 answers

StringBuilder for PHP

Has someone made a StringBuilder implementation in PHP?
Mark
  • 67,098
  • 47
  • 117
  • 162
52
votes
10 answers

Am I undermining the efficiency of StringBuilder?

I've started using StringBuilder in preference to straight concatenation, but it seems like it's missing a crucial method. So, I implemented it myself, as an extension: public void Append(this StringBuilder stringBuilder, params string[] args) { …
dlras2
  • 8,416
  • 7
  • 51
  • 90
50
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,077
  • 1
  • 8
  • 16
47
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 =…
46
votes
3 answers

Performance between String.format and StringBuilder

To concatenate String we often use StringBuilder instead of String + String, but also we can do the same with String.format which returns the formatted string by given locale, format and arguments. Examples: Concatenate the string with…
JUAN CALVOPINA M
  • 3,695
  • 2
  • 21
  • 37
46
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,302
  • 2
  • 13
  • 22
44
votes
10 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
  • 453
  • 1
  • 4
  • 6
42
votes
10 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
  • 8,125
  • 10
  • 46
  • 77
42
votes
6 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,436
  • 3
  • 29
  • 44
42
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
  • 41,155
  • 72
  • 223
  • 385
42
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,326
  • 1
  • 28
  • 46
41
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…
Dumbo
  • 13,555
  • 54
  • 184
  • 288