Java Reference
In-Depth Information
StringBuffer . Concurrent programming will be introduced in Chapter 30. Using String-
Builder is more efficient if it is accessed by just a single task, because no synchronization is
needed in this case. The constructors and methods in StringBuffer and StringBuilder
are almost the same. This section covers StringBuilder . You can replace StringBuilder
in all occurrences in this section by StringBuffer . The program can compile and run with-
out any other changes.
The StringBuilder class has three constructors and more than 30 methods for managing
the builder and modifying strings in the builder. You can create an empty string builder or a
string builder from a string using the constructors, as shown in Figure 10.18.
StringBuilder constructors
java.lang.StringBuilder
+StringBuilder()
+StringBuilder(capacity: int)
+StringBuilder(s: String)
Constructs an empty string builder with capacity 16 .
Constructs a string builder with the specified capacity.
Constructs a string builder with the specified string.
F IGURE 10.18
The StringBuilder class contains the constructors for creating instances
of StringBuilder .
10.11.1 Modifying Strings in the StringBuilder
You can append new contents at the end of a string builder, insert new contents at a speci-
fied position in a string builder, and delete or replace characters in a string builder, using the
methods listed in Figure 10.19.
java.lang.StringBuilder
+append(data: char[]): StringBuilder
+append(data: char[], offset: int, len: int):
StringBuilder
+append(v: aPrimitiveType ): StringBuilder
Appends a char array into this string builder.
Appends a subarray in data into this string builder.
Appends a primitive type value as a string to this
builder.
Appends a string to this string builder.
Deletes characters from startIndex to endIndex-1 .
+append(s: String): StringBuilder
+delete(startIndex: int, endIndex: int):
StringBuilder
+deleteCharAt(index: int): StringBuilder
+insert(index: int, data: char[], offset: int,
len: int): StringBuilder
+insert(offset: int, data: char[]):
StringBuilder
+insert(offset: int, b: aPrimitiveType ):
StringBuilder
+insert(offset: int, s: String): StringBuilder
+replace(startIndex: int, endIndex: int, s:
String): StringBuilder
+reverse(): StringBuilder
+setCharAt(index: int, ch: char): void
Deletes a character at the specified index.
Inserts a subarray of the data in the array into the builder
at the specified index.
Inserts data into this builder at the position offset.
Inserts a value converted to a string into this builder.
Inserts a string into this builder at the position offset.
Replaces the characters in this builder from startIndex
to endIndex-1 with the specified string.
Reverses the characters in the builder.
Sets a new character at the specified index in this
builder.
F IGURE 10.19
The StringBuilder class contains the methods for modifying string builders.
 
 
Search WWH ::




Custom Search