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

C# Differences between operator ==, StringBuilder.Equals, Object.Equals and Object.ReferenceEquals

I have a question about Object.Equals and Equals(object). My sample code is below: class Program { static void Main(string[] args) { var sb1 = new StringBuilder("Food"); var sb2 = new StringBuilder("Food"); …
amitabha
  • 646
  • 1
  • 9
  • 20
26
votes
4 answers

Why doesn't StringBuilder have IndexOf method?

I understand that I can call ToString().IndexOf(...), but I don't want to create an extra string. I understand that I can write a search routine manually. I just wonder why such a routine doesn't already exist in the framework.
thorn0
  • 9,362
  • 3
  • 68
  • 96
25
votes
2 answers

Warning in StringBuffer and StringBuilder

I have a StringBuffer initialized outside for loop and inside for loop I am concatenating some strings. I am getting the warning 'StringBuffer stringBuffer' may be declared as 'StringBuilder' and string concatenation as argument to…
Abish R
  • 1,537
  • 3
  • 18
  • 36
22
votes
3 answers

Append part of Java byte array to StringBuilder

How do I append a portion of byte array to a StringBuilder object under Java? I have a segment of a function that reads from an InputStream into a byte array. I then want to append whatever I read into a StringBuilder object: byte[] buffer = new…
bob
  • 1,941
  • 6
  • 26
  • 36
22
votes
7 answers

Best way to split string into lines with maximum length, without breaking words

I want to break a string up into lines of a specified maximum length, without splitting any words, if possible (if there is a word that exceeds the maximum line length, then it will have to be split). As always, I am acutely aware that strings are…
Late Starter
  • 1,069
  • 2
  • 13
  • 24
22
votes
2 answers

Why does the WPF Presentation library wrap strings in StringBuilder.ToString()?

The code found in the PresentationCore.dll (.NET4 WPF) by ILSpy: // MS.Internal.PresentationCore.BindUriHelper internal static string UriToString(Uri uri) { if (uri == null) { throw new ArgumentNullException("uri"); } return…
Aimeast
  • 1,609
  • 14
  • 29
22
votes
4 answers

Groovy literal StringBuilder/StringBuffer

Groovy supports a literal syntax for creating a StringBuilder/StringBuffer instead of the usual def sb = new StringBuilder() However, I can't seem to remember (or find on Google) the correct syntax.
Dónal
  • 185,044
  • 174
  • 569
  • 824
21
votes
3 answers

When should you explicitly use a StringBuilder?

As I understand it, when I do String baz = "foo" + "bar" + "123" the Java compiler internally replaces the expression with a StringBuilder. However our Java teacher told us that it is good practice to always use a StringBuilder explicitly... Am I…
user2711115
  • 457
  • 3
  • 18
20
votes
2 answers

String.Format vs "string" + "string" or StringBuilder?

Possible Duplicates: Is String.Format as efficient as StringBuilder C# String output: format or concat? What is the performance priority and what should be the conditions to prefer each of the following: String.Format("{0}, {1}", city,…
20
votes
11 answers

String or StringBuilder return values?

If I am building a string using a StringBuilder object in a method, would it make sense to: Return the StringBuilder object, and let the calling code call ToString()? return sb; OR Return the string by calling ToString() myself. return…
John B
  • 20,062
  • 35
  • 120
  • 170
20
votes
3 answers

StringBuilder and Builder Pattern

I'm new in Design Patterns so I have one question about the Builder Pattern. Today I heard that Builder Pattern is different from the class StringBuilder in Java, C#. I know that main goal of the Builder Pattern is to create complex objects in few…
user551761
20
votes
7 answers

Gracefully remove "\n" delimiter after last String within StringBuilder

Have following Java code,that creates StringBuilder with "\n",i.e. carriage return delimiters: while (scanner.hasNextLine()){ sb.append(scanner.nextLine()).append("\n"); } It's occurred,that after last String(line) had "\n" symbol. How to…
sergionni
  • 13,290
  • 42
  • 132
  • 189
20
votes
6 answers

How to check null on StringBuilder?

I want to check for null or empty specifically in my code. Does empty and null are same for StringBuilder in Java? For example: StringBuilder state = new StringBuilder(); StringBuilder err= new StringBuilder(); success = executeCommand(cmd, state,…
user1145280
  • 371
  • 1
  • 2
  • 10
19
votes
6 answers

When to use StringBuilder?

Possible Duplicate: String vs StringBuilder I just revisited some of the books that I used to pick up VB.NET. I am not sure I've got this in my head, understand how/what StringBuilder is. What is the guidance for using? Is it best to use it if…
spacemonkeys
  • 1,689
  • 5
  • 16
  • 29
19
votes
7 answers

How much to grow buffer in a StringBuilder-like C module?

In C, I'm working on a "class" that manages a byte buffer, allowing arbitrary data to be appended to the end. I'm now looking into automatic resizing as the underlying array fills up using calls to realloc. This should make sense to anyone who's…
Brian McFarland
  • 9,052
  • 6
  • 38
  • 56