public final class java.lang
StringBuilder


Hide details Login
Java SE 6
  
Extends: AbstractStringBuilder
Implements: 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 java.lang.StringBuffer be used.
version1.11, 11/17/05
since1.5
See also java.lang.StringBuffer, java.lang.String


Constructors
public StringBuilder()
Constructs a string builder with no characters in it and an initial capacity of 16 characters.
public StringBuilder(int capacity)
Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
capacitythe initial capacity.
ThrowsNegativeArraySizeException: if the capacity argument is less than 0.
public StringBuilder(String str)
Constructs a string builder initialized to the contents of the specified string. The initial capacity of the string builder is 16 plus the length of the string argument.
strthe initial contents of the buffer.
ThrowsNullPointerException: if str is null
public StringBuilder(CharSequence seq)
Constructs a string builder that contains the same characters as the specified CharSequence. The initial capacity of the string builder is 16 plus the length of the CharSequence argument.
seqthe sequence to copy.
ThrowsNullPointerException: if seq is null

Methods
public StringBuilder append(Object obj)
See also valueOf(java.lang.Object), append(java.lang.String)
public StringBuilder append(String str)
public StringBuilder append(StringBuffer sb)
Appends the specified StringBuffer to this sequence.

The characters of the StringBuffer argument are appended, in order, to this sequence, increasing the length of this sequence by the length of the argument. If sb is null, then the four characters "null" are appended to this sequence.

Let n be the length of this character sequence just prior to execution of the append method. Then the character at index k in the new character sequence is equal to the character at index k in the old character sequence, if k is less than n; otherwise, it is equal to the character at index k-n in the argument sb.
sbthe StringBuffer to append.
returna reference to this object.

public StringBuilder append(CharSequence s)
ThrowsIndexOutOfBoundsException:
public StringBuilder append(CharSequence s, int start, int end)
ThrowsIndexOutOfBoundsException: if start or end are negative, or start is greater than end or end is greater than s.length()
public StringBuilder append(char[] str)
public StringBuilder append(char[] str, int offset, int len)
public StringBuilder append(boolean b)
See also valueOf(boolean), append(java.lang.String)
public StringBuilder append(char c)
public StringBuilder append(int i)
See also valueOf(int), append(java.lang.String)
public StringBuilder append(long lng)
See also valueOf(long), append(java.lang.String)
public StringBuilder append(float f)
See also valueOf(float), append(java.lang.String)
public StringBuilder append(double d)
See also valueOf(double), append(java.lang.String)
public StringBuilder appendCodePoint(int codePoint)
since1.5
public StringBuilder delete(int start, int end)
ThrowsStringIndexOutOfBoundsException: if start is negative, greater than length(), or greater than end.
public StringBuilder deleteCharAt(int index)
ThrowsStringIndexOutOfBoundsException: if the index is negative or greater than or equal to length().
public int indexOf(String str)
ThrowsNullPointerException: if str is null.
public int indexOf(String str, int fromIndex)
ThrowsNullPointerException: if str is null.
public StringBuilder insert(int index, char[] str, int offset, int len)
ThrowsStringIndexOutOfBoundsException: if index is negative or greater than length(), or offset or len are negative, or (offset+len) is greater than str.length.
public StringBuilder insert(int offset, Object obj)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also valueOf(java.lang.Object), insert(int, java.lang.String), length()
public StringBuilder insert(int offset, String str)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also length()
public StringBuilder insert(int offset, char[] str)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
public StringBuilder insert(int dstOffset, CharSequence s)
ThrowsIndexOutOfBoundsException: if the offset is invalid.
public StringBuilder insert(int dstOffset, CharSequence s, int start, int end)
ThrowsIndexOutOfBoundsException: if dstOffset is negative or greater than this.length(), or start or end are negative, or start is greater than end or end is greater than s.length()
public StringBuilder insert(int offset, boolean b)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also valueOf(boolean), insert(int, java.lang.String), length()
public StringBuilder insert(int offset, char c)
ThrowsIndexOutOfBoundsException: if the offset is invalid.
See also length()
public StringBuilder insert(int offset, int i)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also valueOf(int), insert(int, java.lang.String), length()
public StringBuilder insert(int offset, long l)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also valueOf(long), insert(int, java.lang.String), length()
public StringBuilder insert(int offset, float f)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also valueOf(float), insert(int, java.lang.String), length()
public StringBuilder insert(int offset, double d)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also valueOf(double), insert(int, java.lang.String), length()
public int lastIndexOf(String str)
ThrowsNullPointerException: if str is null.
public int lastIndexOf(String str, int fromIndex)
ThrowsNullPointerException: if str is null.
public StringBuilder replace(int start, int end, String str)
ThrowsStringIndexOutOfBoundsException: if start is negative, greater than length(), or greater than end.
public StringBuilder reverse()
public String toString()