java stringbuilder code example
Example 1: c# stringbuilder
using System;
using System.Text;
public sealed class App
{
static void Main()
{
StringBuilder sb = new StringBuilder("ABC", 50);
sb.Append(new char[] { 'D', 'E', 'F' });
sb.AppendFormat("GHI{0}{1}", 'J', 'k');
Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString());
sb.Insert(0, "Alphabet: ");
sb.Replace('k', 'K');
Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString());
}
}
Example 2: java string builder
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("string");
System.out.println("String = " + stringBuilder.toString());
Example 3: stringbuilder in java
Java StringBuilder class is used to create
mutable (modifiable) string. The Java
StringBuilder class is same as StringBuffer
class except that it is non-synchronized.
StringBuilder()
creates an empty string Builder with the initial capacity of 16.
StringBuilder(String str)
creates a string Builder with the specified string.
StringBuilder(int length)
creates an empty string Builder with the specified capacity as length.
Example 4: why string builder
StringBuilder is for, well, building strings.
Specifically, building them in a very
performance way. The String class is good
for a lot of things, but it actually has
really terrible performance when assembling
a new string out of smaller string parts
because each new string is a totally new,
reallocated string.
Example 5: stringbuilder
It is mutable char sequence and it is not
thread safe.