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

In .NET 2.0 it uses the String class internally. String is only immutable outside of the System namespace, so StringBuilder can do that. In .NET 4.0 String was changed to use char[]. In 2.0 StringBuilder looked like this public sealed class StringBuilder : ISerializable { // Fields private const string CapacityField = “Capacity”; internal const … Read more

StringBuffer is obsolete?

It’s obsolete in that new code on Java 1.5 should generally use StringBuilder – it’s very rare that you really need to build strings in a thread-safe manner, so why pay the synchronization cost? I suspect code that you see using StringBuffer mostly falls into buckets of: Written before Java 1.5 Written to maintain compatibility … Read more

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

Ok I will suppose a general use, first you have to download apache commons, there you will find an utility class named IOUtils which has a method named copy(); Now the solution is: get the input stream of your CLOB object using getAsciiStream() and pass it to the copy() method. InputStream in = clobObject.getAsciiStream(); StringWriter … Read more

How to append two stringBuilders?

I know this is three years later, but the .NET 4 StringBuilder behaves differently anyway. Nevertheless, it does still come back to “what do you want to do?” Are you looking for simply the most performant way of appending two StringBuilders and continuing on with just the latter result? Or are you expecting to continue … Read more

When do you use StringBuilder.AppendLine/string.Format vs. StringBuilder.AppendFormat?

I view AppendFormat followed by AppendLine as not only more readable, but also more performant than calling AppendLine(string.Format(…)). The latter creates a whole new string and then appends it wholesale into the existing builder. I’m not going to go as far as saying “Why bother using StringBuilder then?” but it does seem a bit against … Read more

Write StringBuilder to Stream

Don’t use a StringBuilder, if you’re writing to a stream, do just that with a StreamWriter: using (var memoryStream = new MemoryStream()) using (var writer = new StreamWriter(memoryStream )) { // Various for loops etc as necessary that will ultimately do this: writer.Write(…); }

What is the difference between String and StringBuffer in Java?

String is used to manipulate character strings that cannot be changed (read-only and immutable). StringBuffer is used to represent characters that can be modified. Performance wise, StringBuffer is faster when performing concatenations. This is because when you concatenate a String, you are creating a new object (internally) every time since String is immutable. You can … Read more

What is the simplest way to write the contents of a StringBuilder to a text file in .NET 1.1?

You still have access to StreamWriter: using (System.IO.StreamWriter file = new System.IO.StreamWriter(@”\hereIam.txt”)) { file.WriteLine(sb.ToString()); // “sb” is the StringBuilder } From the MSDN documentation: Writing to a Text File (Visual C#). For newer versions of the .NET Framework (Version 2.0. onwards), this can be achieved with one line using the File.WriteAllText method. System.IO.File.WriteAllText(@”C:\TextFile.txt”, stringBuilder.ToString());