StringBuilder Class
Package: java.lang
StringBuilder
represents a mutable sequence of characters. That is, a string that can be changed. This sets it apart from the String
class, which represents an immutable sequence of characters. StringBuilder
can be more efficient than String in applications that do a lot of string maniuplations.
Constructors
Constructor |
Description |
|
Creates a new empty |
|
Creates a new |
Methods
Method |
Description |
|
Appends the string representation of the primitive type to the end of the string. |
|
Calls the object’s |
|
Appends the string to the end of the |
|
Returns the character at the specified position in the string. |
|
Deletes characters starting with the first |
|
Deletes the character at the specified position. |
|
Ensures that the capacity of |
|
Returns the capacity of this |
|
Returns the index of the first occurrence of the specified string. If the string doesn’t appear, returns |
|
Returns the index of the first occurrence of the specified string, starting the search at the specified index position. If the string doesn’t appear, returns |
|
Inserts the string representation of the primitive type at the point specified by the |
|
Calls the |
|
Inserts the string at the point specified by the |
|
Returns the index of the last occurrence of the specified string. If the string doesn’t appear, returns |
|
Returns the index of the last occurrence of the specified string, starting the search at the specified index position. If the string doesn’t appear, returns |
|
Returns the length of this string. |
|
Replaces the substring indicated by the first two parameters with the string provided by the third parameter. |
|
Reverses the order of characters. |
|
Sets the character at the specified position to the specified character. |
|
Sets the length of the string. If that length is less than the current length, the string is truncated; if it’s greater than the current length, new characters — hexadecimal zeros — are added. |
|
Extracts a substring, beginning at the position indicated by the |
|
Extracts a substring, beginning at the position indicated by the first parameter and ending at the position one character before the value of the second parameter. |
|
Returns the current value as a |
|
Reduces the capacity of the |
Creating a StringBuilder object
You can’t assign string literals directly to a StringBuilder
object like you can with a String
object. The StringBuilder
class, however, has a constructor that accepts a String
as a parameter. So to create a StringBuilder
object, you use a statement such as this:
StringBuilder sb = new StringBuilder(“Today is the day!”);
Internally, a StringBuilder
object maintains a fixed area of memory where it stores a string value. This area of memory is the buffer. The string held in this buffer doesn’t have to use the entire buffer. As a result, a StringBuilder
object has both a length and a capacity. The length represents the current length of the string maintained by the StringBuilder
, and the capacity represents the size of the buffer itself. Note that the length can’t exceed the capacity.
When you create a StringBuilder
object, initially the capacity is set to the length of the string plus 16. The StringBuilder
class automatically increases its capacity whenever necessary, so you don’t have to worry about exceeding the capacity.