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
41
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
  • 8,333
  • 17
  • 71
  • 94
40
votes
3 answers

C# String.Substring equivalent for StringBuilder?

StringBuilder does not appear to have a Substring(start, len) method... what am I missing here?
James
  • 1,973
  • 1
  • 18
  • 32
38
votes
3 answers

PowerShell And StringBuilder

I am new in PowerShell but am familiar with .NET classes. I am using System.Text.StringBuilder in PowerShell script. The script is that Function MyStringFunc([String]$line) { $r = New-Object -TypeName…
Alex Yeung
  • 2,495
  • 7
  • 31
  • 48
38
votes
8 answers

Default capacity of StringBuilder

What is the default capacity of a StringBuilder? And when should (or shouldn't) the default be used?
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
38
votes
1 answer

StringBuilder.ToString() throw an 'Index out of range' Exception

I would really appreciate someone help me resolving the following issue: I am getting now and then the following exception: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: chunkLength on…
Gerhard Powell
  • 5,965
  • 5
  • 48
  • 59
37
votes
2 answers

Is using a StringBuilder a right thing to do in F#?

StringBuiler is a mutable object, F# encourages employing immutability as much as possible. So one should use transformation rather than mutation. Does this apply to StringBuilder when it comes to building a string in F#? Is there an F# immutable…
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
36
votes
10 answers

How to retrieve a StringBuilder Line Count?

I have a StringBuilder instance where I am doing numerous sb.AppendLine("test"); for example. How do I work out how many lines I have? I see the class has .Length but that tells me how many characters in all. Any ideas?
Jon
  • 38,814
  • 81
  • 233
  • 382
36
votes
4 answers

StringBuilder Vs StringWriter/StringReader

I recently read that in StringWriter and StringReader are used for writing and reading from StringBuilder. Well when I use StringBuilder Object, it looks to be a self sufficient class. We have every way of reading and writing the StringBuilder,…
SaravananArumugam
  • 3,680
  • 6
  • 33
  • 45
36
votes
4 answers

What does ----s mean in the context of StringBuilder.ToString()?

The Reference Source page for stringbuilder.cs has this comment in the ToString method: if (chunk.m_ChunkLength > 0) { // Copy these into local variables so that they // are stable even in the presence of ----s (hackers might do this) …
Micteu
  • 451
  • 5
  • 17
35
votes
3 answers

Is .NET's StringBuilder thread-safe

The regular "Thread Safety" section of the MSDN documentation for StringBuilder states that: ...any instance members are not guaranteed to be thread safe... but this statement feels like it has been copied and pasted for almost every class in the…
Jason Stangroome
  • 4,459
  • 3
  • 33
  • 39
35
votes
9 answers

StringBuilder vs. String considering replace

When doing concatenating lots of strings, I have been recommended to do it using a StringBuilder as such: StringBuilder someString = new StringBuilder("abc"); someString.append("def"); someString.append("123"); someString.append("moreStuff"); as…
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
33
votes
4 answers

What is an efficient way to compare StringBuilder objects

Well I have two StringBuilder objects, I need to compare them in Java. One way I know I can do is sb1.toString().equals(sb2.toString()); but that means I am creating two String objects, is there any better way to compare StringBuilder objects.…
user1344389
  • 437
  • 1
  • 6
  • 9
32
votes
6 answers

Java CharAt() and deleteCharAt() performance

I've been wondering about the implementation of charAt function for String/StringBuilder/StringBuffer in java what is the complexity of that ? also what about the deleteCharAt() in StringBuffer/StringBuilder ?
Jimmar
  • 4,194
  • 2
  • 28
  • 43
32
votes
5 answers

"Don't use StringBuilder or foreach in this hot code path"

I am browsing the source code of the Open Source SignalR project, and I see this diff code which is entitled "Don't use StringBuilder or foreach in this hot code path" : - public static string MakeCursor(IEnumerable cursors) + …
Larry
  • 17,605
  • 9
  • 77
  • 106
31
votes
5 answers

Forcing StreamWriter to change Encoding

I am trying to save a file using DialogResult and StringBuilder. After making the text, I am calling the following code to save the file: if (dr == DialogResult.OK) { StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); …
Dumbo
  • 13,555
  • 54
  • 184
  • 288