sb.append("qwertyuiop");
sb.append(' ');
sb.append(1);
sb.append(false);
sb.append(2.35);
System.out.println(sb);Output from running code snippet:
qwertyuiopqwertyuiop 1false2.35
StringBuilder insert(int offset, String str)
- inserts the characters in the specified String
at the specified index.StringBuilder sb = new StringBuilder("qwertyuiop");
sb.insert(3,"AB");
System.out.println(sb);
qweABrtyuiop
StringBuilder replace(int start, int end, String str)
- replaces the characters between the specified start and end points with the characters in the specified String
.StringBuilder sb = new StringBuilder("qwertyuiop");
sb.replace(3,6,"AB");
System.out.println(sb);
qweABuiop
StringBuilder delete(int start, int end)
- removes the characters between the specified start and end points.StringBuilder sb = new StringBuilder("qwertyuiop");
sb.delete(3,6);
System.out.println(sb);
qweuiop
StringBuilder reverse()
- reverses the sequence of characters.StringBuilder sb = new StringBuilder("qwertyuiop");
sb.reverse();
System.out.println(sb);
poiuytrewq
Most of the methods of the StringBuilder
class return a reference to themselves - allowing multiple operations on a StringBuilder
instance to be chained together in a single statement. This technique is known as method chaining.
StringBuilder
.StringBuilder sb = new StringBuilder("qwerty");
sb.reverse().append("abcdef").insert(8, "Z");
System.out.println(sb);
ytrewqabZcdef
StringBuilder
When performing concatenation of lots of String
objects (e.g. in a loop), a StringBuilder
can provide better performance then using the +
operator. A StringBuilder
can be more efficient as it creates less objects.
long start = System.nanoTime();
String s = "";
for (int i=0; i<5000; i++) {
s += i;
}
long duration = System.nanoTime()-start;
System.out.println(duration + " nanoseconds for String version");
start = System.nanoTime();
StringBuilder sb = new StringBuilder();
for (int i=0; i<5000; i++) {
sb.append(i);
}
duration = System.nanoTime()-start;
System.out.println(duration + " nanoseconds for StringBuilder version");
start = System.nanoTime();
sb = new StringBuilder(18890);
for (int i=0; i<5000; i++) {
sb.append(i);
}
duration = System.nanoTime()-start;
System.out.println(duration + " nanoseconds for StringBuilder with large initial capacity version");
308243046 nanoseconds for String version
972206 nanoseconds for StringBuilder version
581890 nanoseconds for StringBuilder with large initial capacity version
java.lang.StringBuffer
classBefore StringBuilder
was introduced in Java 5 the java.lang.StringBuffer
class was the standard way of working with modifiable sequences of characters. Both StringBuffer
and StringBuilder
extend java.lang.AbstractStringBuilder
. The significant difference between the two classes is that StringBuffer
is synchronised while StringBuilder
is not. The lack of syncronisation makes StringBuilder
faster and therefore the recommended choice for single-threaded use. If the object is to be used by multiple threades then StringBuffer
is the recommended option.
StringBuffer sb = new StringBuffer("qwertyuiop");
sb.append("qwertyuiop");
sb.append(' ');
sb.append(1);
sb.append(false);
sb.append(2.35);
System.out.println(sb);
qwertyuiopqwertyuiop 1false2.35