# StringBuilder
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. It is available since JDK 1.5.
# Comparing StringBuffer, StringBuilder, Formatter and StringJoiner
The StringBuffer
, StringBuilder
, Formatter
and StringJoiner
classes are Java SE utility classes that are primarily used for assembling strings from other information:
This example shows how StringBuilder
is can be used:
int one = 1;
String color = "red";
StringBuilder sb = new StringBuilder();
sb.append("One=").append(one).append(", Color=").append(color).append('\n');
System.out.print(sb);
// Prints "One=1, Colour=red" followed by an ASCII newline.
(The StringBuffer
class is used the same way: just change StringBuilder
to StringBuffer
in the above)
The StringBuffer
and StringBuilder
classes are suitable for both assembling and modifying strings; i.e they provide methods for replacing and removing characters as well as adding them in various. The remining two classes are specific to the task of assembling strings.
Here are some typical examples of Formatter
usage:
// This does the same thing as the StringBuilder example above
int one = 1;
String color = "red";
Formatter f = new Formatter();
System.out.print(f.format("One=%d, colour=%s%n", one, color));
// Prints "One=1, Colour=red" followed by the platform's line separator
// The same thing using the `String.format` convenience method
System.out.print(String.format("One=%d, color=%s%n", one, color));
The StringJoiner
class is not ideal for the above task, so here is an example of a formatting an array of strings.
StringJoiner sj = new StringJoiner(", ", "[", "]");
for (String s : new String[]{"A", "B", "C"}) {
sj.add(s);
}
System.out.println(sj);
// Prints "[A, B, C]"
The use-cases for the 4 classes can be summarized:
StringBuilder
suitable for any string assembly OR string modification task.StringBuffer
use (only) when you require a thread-safe version ofStringBuilder
.Formatter
provides much richer string formatting functionality, but is not as efficient asStringBuilder
. This is because each call toFormatter.format(...)
entails:- parsing the
format
string, - creating and populate a varargs array, and
- autoboxing any primitive type arguments.
# Repeat a String n times
Problem: Create a String
containing n
repetitions of a String s
.
The trivial approach would be repeatedly concatenating the String
final int n = ...
final String s = ...
String result = "";
for (int i = 0; i < n; i++) {
result += s;
}
This creates n
new string instances containing 1 to n
repetitions of s
resulting in a runtime of O(s.length() * n²) = O(s.length() * (1+2+...+(n-1)+n))
.
To avoid this StringBuilder
should be used, which allows creating the String
in O(s.length() * n)
instead:
final int n = ...
final String s = ...
StringBuilder builder = new StringBuilder();
for (int i = 0; i < n; i++) {
builder.append(s);
}
String result = builder.toString();
# Syntax
# Remarks
Creating a new StringBuilder
with type char
as a parameter would result in calling the constructor with argument int capacity
and not the one with argument String string
:
StringBuilder v = new StringBuilder('I'); //'I' is a character, "I" is a String.
System.out.println(v.capacity()); --> output 73
System.out.println(v.toString()); --> output nothing