StringBuilder

public final class StringBuilder extends Object
implements Appendable CharSequence Serializable CharSequence

A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.

For example, if z refers to a string builder object whose current contents are "start", then the method call z.append("le") would cause the string builder to contain "startle", whereas z.insert(4, "le") would alter the string builder to contain "starlet".

In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

Public Constructor Summary

StringBuilder()
Constructs a string builder with no characters in it and an initial capacity of 16 characters.
StringBuilder(int capacity)
Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
StringBuilder(String str)
Constructs a string builder initialized to the contents of the specified string.
StringBuilder(CharSequence seq)
Constructs a string builder that contains the same characters as the specified CharSequence.

Public Method Summary

StringBuilder
append(boolean b)
StringBuilder
append(long lng)
StringBuilder
append(char c)
Appends the specified character to this Appendable.
StringBuilder
append(Object obj)
StringBuilder
append(char[] str, int offset, int len)
StringBuilder
append(double d)
StringBuilder
append(char[] str)
StringBuilder
append(String str)
StringBuilder
append(StringBuffer sb)
Appends the specified StringBuffer to this sequence.
StringBuilder
append(float f)
StringBuilder
append(int i)
StringBuilder
append(CharSequence s, int start, int end)
Appends a subsequence of the specified character sequence to this Appendable.
StringBuilder
append(CharSequence s)
Appends the specified character sequence to this Appendable.
StringBuilder
appendCodePoint(int codePoint)
int
char
charAt(int i)
Returns the char value at the specified index.
int
codePointAt(int i)
int
int
codePointCount(int i, int j)
StringBuilder
delete(int start, int end)
StringBuilder
deleteCharAt(int index)
void
void
getChars(int i, int j, char[] buf, int k)
int
int
indexOf(String s, int i)
StringBuilder
insert(int offset, char[] str)
StringBuilder
insert(int offset, float f)
StringBuilder
insert(int dstOffset, CharSequence s)
StringBuilder
insert(int offset, char c)
StringBuilder
insert(int offset, long l)
StringBuilder
insert(int index, char[] str, int offset, int len)
StringBuilder
insert(int offset, int i)
StringBuilder
insert(int offset, String str)
StringBuilder
insert(int offset, double d)
StringBuilder
insert(int dstOffset, CharSequence s, int start, int end)
StringBuilder
insert(int offset, Object obj)
StringBuilder