Android APIs
public final class

StringBuffer

extends Object
implements Appendable Serializable CharSequence
java.lang.Object
   ↳ java.lang.StringBuffer

Class Overview

A modifiable sequence of characters for use in creating strings, where all accesses are synchronized. This class has mostly been replaced by StringBuilder because this synchronization is rarely useful. This class is mainly used to interact with legacy APIs that expose it.

For particularly complex string-building needs, consider Formatter.

The majority of the modification methods on this class return this so that method calls can be chained together. For example: new StringBuffer("a").append("b").append("c").toString().

Summary

Public Constructors
StringBuffer()
Constructs a new StringBuffer using the default capacity which is 16.
StringBuffer(int capacity)
Constructs a new StringBuffer using the specified capacity.
StringBuffer(String string)
Constructs a new StringBuffer containing the characters in the specified string.
StringBuffer(CharSequence cs)
Constructs a StringBuffer and initializes it with the content from the specified CharSequence.
Public Methods
StringBuffer append(char[] chars, int start, int length)
Adds the specified sequence of characters to the end of this buffer.
StringBuffer append(double d)
Adds the string representation of the specified double to the end of this StringBuffer.
StringBuffer append(char ch)
Adds the specified character to the end of this buffer.
StringBuffer append(long l)
Adds the string representation of the specified long to the end of this StringBuffer.
StringBuffer append(boolean b)
Adds the string representation of the specified boolean to the end of this StringBuffer.
StringBuffer append(float f)
Adds the string representation of the specified float to the end of this StringBuffer.
StringBuffer append(char[] chars)
Adds the character array to the end of this buffer.
StringBuffer append(int i)
Adds the string representation of the specified integer to the end of this StringBuffer.
StringBuffer append(String string)
Adds the specified string to the end of this buffer.
StringBuffer append(CharSequence s, int start, int end)
Appends the specified subsequence of the CharSequence to this buffer.
StringBuffer append(StringBuffer sb)
Adds the specified StringBuffer to the end of this buffer.
StringBuffer append(Object obj)
Adds the string representation of the specified object to the end of this StringBuffer.
StringBuffer append(CharSequence s)
Appends the specified CharSequence to this buffer.
StringBuffer appendCodePoint(int codePoint)
Appends the string representation of the specified Unicode code point to the end of this buffer.
int capacity()
Returns the number of characters that can be held without growing.
char charAt(int index)
Returns the character at index.
int codePointAt(int index)
Retrieves the Unicode code point value at the index.
int codePointBefore(int index)
Retrieves the Unicode code point value that precedes the index.
int codePointCount(int beginIndex, int endIndex)
Calculates the number of Unicode code points between start and end.
StringBuffer delete(int start, int end)
Deletes a range of characters.
StringBuffer deleteCharAt(int location)
Deletes the character at the specified offset.
void ensureCapacity(int min)
Ensures that this object has a minimum capacity available before requiring the internal buffer to be enlarged.
void getChars(int start, int end, char[] buffer, int idx)
Copies the requested sequence of characters to the char[] passed starting at idx.
int indexOf(String subString, int start)
Searches for the index of the specified character.
int indexOf(String string)
Searches for the first index of the specified character.
StringBuffer insert(int index, CharSequence s, int start, int end)
Inserts the specified subsequence into this buffer at the specified index.
StringBuffer insert(int index, Object obj)
Inserts the string representation of the specified object into this buffer at the specified offset.
StringBuffer insert(int index, float f)
Inserts the string representation of the specified float into this buffer at the specified offset.
StringBuffer insert(int index, char ch)
Inserts the character into this buffer at the specified offset.
StringBuffer insert(int index, int i)
Inserts the string representation of the specified integer into this buffer at the specified offset.
StringBuffer insert(int index, char[] chars)
Inserts the character array into this buffer at the specified offset.
StringBuffer insert(int index, double d)
Inserts the string representation of the specified into this buffer double at the specified offset.
StringBuffer insert(int index, CharSequence s)
Inserts the specified CharSequence into this buffer at the specified index.
StringBuffer insert(int index, char[] chars, int start, int length)
Inserts the specified subsequence of characters into this buffer at the specified index.
StringBuffer insert(int index, String string)
Inserts the string into this buffer at the specified offset.
StringBuffer insert(int index, long l)
Inserts the string representation of the specified long into this buffer at the specified offset.
StringBuffer insert(int index, boolean b)
Inserts the string representation of the specified boolean into this buffer at the specified offset.
int lastIndexOf(String subString, int start)
Searches for the index of the specified character.
int lastIndexOf(String string)
Searches for the last index of the specified character.
int length()
The current length.
int offsetByCodePoints(int index, int codePointOffset)
Returns the index that is offset codePointOffset code points from index.
StringBuffer replace(int start, int end, String string)
Replaces the characters in the specified range with the contents of the specified string.
StringBuffer reverse()
Reverses the order of characters in this buffer.
void setCharAt(int index, char ch)
Sets the character at the index.
void setLength(int length)
Sets the current length to a new value.
CharSequence subSequence(int start, int end)
Returns a CharSequence of the subsequence from the start index to the end index.
String substring(int start)
Returns the String value of the subsequence from the start index to the current end.
String substring(int start, int end)
Returns the String value of the subsequence from the start index to the end index.
String toString()
Returns the current String representation.
void trimToSize()
Trims off any extra capacity beyond the current length.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.Appendable
From interface java.lang.CharSequence

Public Constructors

public StringBuffer ()

Added in API level 1

Constructs a new StringBuffer using the default capacity which is 16.

public StringBuffer (int capacity)

Added in API level 1

Constructs a new StringBuffer using the specified capacity.

Parameters
capacity int: the initial capacity.

public StringBuffer (String string)

Added in API level 1

Constructs a new StringBuffer containing the characters in the specified string. The capacity of the new buffer will be the length of the String plus the default capacity.

Parameters
string String: the string content with which to initialize the new instance.
Throws
NullPointerException if string is null.

public StringBuffer (CharSequence cs)

Added in API level 1

Constructs a StringBuffer and initializes it with the content from the specified CharSequence. The capacity of the new buffer will be the length of the CharSequence plus the default capacity.

Parameters
cs CharSequence: the content to initialize the instance.
Throws
NullPointerException if cs is null.

Public Methods

public StringBuffer append (char[] chars, int start, int length)

Added in API level 1

Adds the specified sequence of characters to the end of this buffer.

Parameters
chars char: the character array to append.
start int: the starting offset.
length int: the number of characters.
Returns
StringBuffer this StringBuffer.
Throws
ArrayIndexOutOfBoundsException if length < 0 , start < 0 or start + length > chars.length.
NullPointerException if chars is null.

public StringBuffer append (double d)

Added in API level 1

Adds the string representation of the specified double to the end of this StringBuffer.

Parameters
d double: the double to append.
Returns
StringBuffer this StringBuffer.

See also:

public StringBuffer append (char ch)

Added in API level 1

Adds the specified character to the end of this buffer.

Parameters
ch char: the character to append.
Returns
StringBuffer this StringBuffer.

See also:

public StringBuffer append (long l)

Added in API level 1

Adds the string representation of the specified long to the end of this StringBuffer.

Parameters
l long: the long to append.
Returns
StringBuffer this StringBuffer.

See also:

public StringBuffer append (boolean b)

Added in API level 1

Adds the string representation of the specified boolean to the end of this StringBuffer.

If the argument is true the string "true" is appended, otherwise the string "false" is appended.

Parameters
b boolean: the boolean to append.
Returns
StringBuffer this StringBuffer.

See also:

public StringBuffer append (float f)

Added in API level 1

Adds the string representation of the specified float to the end of this StringBuffer.

Parameters
f float: the float to append.
Returns
StringBuffer this StringBuffer.

See also:

public StringBuffer append (char[] chars)

Added in API level 1

Adds the character array to the end of this buffer.

Parameters
chars char: the character array to append.
Returns
StringBuffer this StringBuffer.
Throws
NullPointerException if chars is null.

public StringBuffer append (int i)

Added in API level 1

Adds the string representation of the specified integer to the end of this StringBuffer.

Parameters
i int: the integer to append.
Returns
StringBuffer this StringBuffer.

See also:

public StringBuffer append (String string)

Added in API level 1

Adds the specified string to the end of this buffer.

If the specified string is null the string "null" is appended, otherwise the contents of the specified string is appended.

Parameters
string String: the string to append (may be null).
Returns
StringBuffer this StringBuffer.

public StringBuffer append (CharSequence s, int start, int end)

Added in API level 1

Appends the specified subsequence of the CharSequence to this buffer.

If the specified CharSequence is null, then the string "null" is used to extract a subsequence.

Parameters
s CharSequence: the CharSequence to append.
start int: the inclusive start index.
end int: the exclusive end index.
Returns
StringBuffer this StringBuffer.
Throws
IndexOutOfBoundsException if start or end are negative, start is greater than end or end is greater than the length of s.

public StringBuffer append (StringBuffer sb)

Added in API level 1

Adds the specified StringBuffer to the end of this buffer.

If the specified StringBuffer is null the string "null" is appended, otherwise the contents of the specified StringBuffer is appended.

Parameters
sb StringBuffer: the StringBuffer to append (may be null).
Returns
StringBuffer this StringBuffer.

public StringBuffer append (Object obj)

Added in API level 1

Adds the string representation of the specified object to the end of this StringBuffer.

If the specified object is null the string "null" is appended, otherwise the objects toString is used to get its string representation.

Parameters
obj Object: the object to append (may be null).
Returns
StringBuffer this StringBuffer.

See also:

public StringBuffer append (CharSequence s)

Added in API level 1

Appends the specified CharSequence to this buffer.

If the specified CharSequence is null the string "null" is appended, otherwise the contents of the specified CharSequence is appended.

Parameters
s CharSequence: the CharSequence to append.
Returns
StringBuffer this StringBuffer.

public StringBuffer appendCodePoint (int codePoint)

Added in API level 1

Appends the string representation of the specified Unicode code point to the end of this buffer.

The code point is converted to a char[] as defined by toChars(int).

Parameters
codePoint int: the Unicode code point to encode and append.
Returns
StringBuffer this StringBuffer.

See also:

public int capacity ()

Added in API level 1

Returns the number of characters that can be held without growing.

Returns
int the capacity

public char charAt (int index)

Added in API level 1

Returns the character at index.

Parameters
index int
Returns
char

public int codePointAt (int index)

Added in API level 1

Retrieves the Unicode code point value at the index.

Parameters
index int: the index to the char code unit.
Returns
int the Unicode code point value.

public int codePointBefore (int index)

Added in API level 1

Retrieves the Unicode code point value that precedes the index.

Parameters
index int: the index to the char code unit within this object.
Returns
int the Unicode code point value.

public int codePointCount (int beginIndex, int endIndex)

Added in API level 1

Calculates the number of Unicode code points between start and end.

Parameters
beginIndex int: the inclusive beginning index of the subsequence.
endIndex int: the exclusive end index of the subsequence.
Returns
int the number of Unicode code points in the subsequence.

public StringBuffer delete (int start, int end)

Added in API level 1

Deletes a range of characters.

Parameters
start int: the offset of the first character.
end int: the offset one past the last character.
Returns
StringBuffer this StringBuffer.
Throws
StringIndexOutOfBoundsException if start < 0, start > end or end > length().

public StringBuffer deleteCharAt (int location)

Added in API level 1

Deletes the character at the specified offset.

Parameters
location int: the offset of the character to delete.
Returns
StringBuffer this StringBuffer.
Throws
StringIndexOutOfBoundsException if location < 0 or location >= length()

public void ensureCapacity (int min)

Added in API level 1

Ensures that this object has a minimum capacity available before requiring the internal buffer to be enlarged. The general policy of this method is that if the minimumCapacity is larger than the current capacity(), then the capacity will be increased to the largest value of either the minimumCapacity or the current capacity multiplied by two plus two. Although this is the general policy, there is no guarantee that the capacity will change.

Parameters
min int: the new minimum capacity to set.

public void getChars (int start, int end, char[] buffer, int idx)

Added in API level 1

Copies the requested sequence of characters to the char[] passed starting at idx.

Parameters
start int: the starting offset of characters to copy.
end int: the ending offset of characters to copy.
buffer char: the destination character array.
idx int: the starting offset in the character array.
Throws
IndexOutOfBoundsException if start < 0, end > length(), start > end, index < 0, end - start > buffer.length - index

public int indexOf (String subString, int start)

Added in API level 1

Searches for the index of the specified character. The search for the character starts at the specified offset and moves towards the end.

Parameters
subString String: the string to find.
start int: the starting offset.
Returns
int the index of the specified character, -1 if the character isn't found

public int indexOf (String string)

Added in API level 1

Searches for the first index of the specified character. The search for the character starts at the beginning and moves towards the end.

Parameters
string String: the string to find.
Returns
int the index of the specified character, -1 if the character isn't found.

public StringBuffer insert (int index, CharSequence s, int start, int end)

Added in API level 1

Inserts the specified subsequence into this buffer at the specified index.

If the specified CharSequence is null, the string "null" is inserted, otherwise the contents of the CharSequence.

Parameters
index int: The index at which to insert.
s CharSequence: The char sequence to insert.
start int: The inclusive start index in the char sequence.
end int: The exclusive end index in the char sequence.
Returns
StringBuffer this buffer.
Throws
IndexOutOfBoundsException if index is negative or greater than the current length, start or end are negative, start is greater than end or end is greater than the length of s.

public StringBuffer insert (int index, Object obj)

Added in API level 1

Inserts the string representation of the specified object into this buffer at the specified offset.

If the specified object is null, the string "null" is inserted, otherwise the objects toString method is used to get its string representation.

Parameters
index int: the index at which to insert.
obj Object: the object to insert (may be null).
Returns
StringBuffer this buffer.
Throws
StringIndexOutOfBoundsException if index < 0 or index > length().

public StringBuffer insert (int index, float f)

Added in API level 1

Inserts the string representation of the specified float into this buffer at the specified offset.

Parameters
index int: the index at which to insert.
f float: the float to insert.
Returns
StringBuffer this buffer.
Throws
StringIndexOutOfBoundsException if index < 0 or index > length().

public StringBuffer insert (int index, char ch)

Added in API level 1

Inserts the character into this buffer at the specified offset.

Parameters
index int: the index at which to insert.
ch char: the character to insert.
Returns
StringBuffer this buffer.
Throws
ArrayIndexOutOfBoundsException if index < 0 or index > length().

public StringBuffer insert (int index, int i)

Added in API level 1

Inserts the string representation of the specified integer into this buffer at the specified offset.

Parameters
index int: the index at which to insert.
i int: the integer to insert.
Returns
StringBuffer this buffer.
Throws
StringIndexOutOfBoundsException if index < 0 or index > length().

public StringBuffer insert (int index, char[] chars)

Added in API level 1

Inserts the character array into this buffer at the specified offset.

Parameters
index int: the index at which to insert.
chars char: the character array to insert.
Returns
StringBuffer this buffer.
Throws
StringIndexOutOfBoundsException if index < 0 or index > length().
NullPointerException if chars is null.

public StringBuffer insert (int index, double d)

Added in API level 1

Inserts the string representation of the specified into this buffer double at the specified offset.

Parameters
index int: the index at which to insert.
d double: the double to insert.
Returns
StringBuffer this buffer.
Throws
StringIndexOutOfBoundsException if index < 0 or index > length().

public StringBuffer insert (int index, CharSequence s)

Added in API level 1

Inserts the specified CharSequence into this buffer at the specified index.

If the specified CharSequence is null, the string "null" is inserted, otherwise the contents of the CharSequence.

Parameters
index int: The index at which to insert.
s CharSequence: The char sequence to insert.
Returns
StringBuffer this buffer.
Throws
IndexOutOfBoundsException if index < 0 or index > length().

public StringBuffer insert (int index, char[] chars, int start, int length)

Added in API level 1

Inserts the specified subsequence of characters into this buffer at the specified index.

Parameters
index int: the index at which to insert.
chars char: the character array to insert.
start int: the starting offset.
length int: the number of characters.
Returns
StringBuffer this buffer.
Throws
NullPointerException if chars is null.
StringIndexOutOfBoundsException if length < 0, start < 0, start + length > chars.length, index < 0 or index > length()

public StringBuffer insert (int index, String string)

Added in API level 1

Inserts the string into this buffer at the specified offset.

If the specified string is null, the string "null" is inserted, otherwise the contents of the string is inserted.

Parameters
index int: the index at which to insert.
string String: the string to insert (may be null).
Returns
StringBuffer this buffer.
Throws
StringIndexOutOfBoundsException if index < 0 or index > length().

public StringBuffer insert (int index, long l)

Added in API level 1

Inserts the string representation of the specified long into this buffer at the specified offset.

Parameters
index int: the index at which to insert.
l long: the long to insert.
Returns
StringBuffer this buffer.
Throws
StringIndexOutOfBoundsException if index < 0 or index > length().

public StringBuffer insert (int index, boolean b)

Added in API level 1

Inserts the string representation of the specified boolean into this buffer at the specified offset.

Parameters
index int: the index at which to insert.
b boolean: the boolean to insert.
Returns
StringBuffer this buffer.
Throws
StringIndexOutOfBoundsException if index < 0 or index > length().

public int lastIndexOf (String subString, int start)

Added in API level 1

Searches for the index of the specified character. The search for the character starts at the specified offset and moves towards the beginning.

Parameters
subString String: the string to find.
start int: the starting offset.
Returns
int the index of the specified character, -1 if the character isn't found.

public int lastIndexOf (String string)

Added in API level 1

Searches for the last index of the specified character. The search for the character starts at the end and moves towards the beginning.

Parameters
string String: the string to find.
Returns
int the index of the specified character, -1 if the character isn't found.
Throws
NullPointerException if string is null.

public int length ()

Added in API level 1

The current length.

Returns
int the number of characters contained in this instance.

public int offsetByCodePoints (int index, int codePointOffset)

Added in API level 1

Returns the index that is offset codePointOffset code points from index.

Parameters
index int: the index to calculate the offset from.
codePointOffset int: the number of code points to count.
Returns
int the index that is codePointOffset code points away from index.

public StringBuffer replace (int start, int end, String string)

Added in API level 1

Replaces the characters in the specified range with the contents of the specified string.

Parameters
start int: the inclusive begin index.
end int: the exclusive end index.
string String: the string that will replace the contents in the range.
Returns
StringBuffer this buffer.
Throws
StringIndexOutOfBoundsException if start or end are negative, start is greater than end or end is greater than the length of s.

public StringBuffer reverse ()

Added in API level 1

Reverses the order of characters in this buffer.

Returns
StringBuffer this buffer.

public void setCharAt (int index, char ch)

Added in API level 1

Sets the character at the index.

Parameters
index int: the zero-based index of the character to replace.
ch char: the character to set.

public void setLength (int length)

Added in API level 1

Sets the current length to a new value. If the new length is larger than the current length, then the new characters at the end of this object will contain the char value of .

Parameters
length int: the new length of this StringBuffer.

public CharSequence subSequence (int start, int end)

Added in API level 1

Returns a CharSequence of the subsequence from the start index to the end index.

Parameters
start int: the inclusive start index to begin the subsequence.
end int: the exclusive end index to end the subsequence.
Returns
CharSequence a CharSequence containing the subsequence.

public String substring (int start)

Added in API level 1

Returns the String value of the subsequence from the start index to the current end.

Parameters
start int: the inclusive start index to begin the subsequence.
Returns
String a String containing the subsequence.

public String substring (int start, int end)

Added in API level 1

Returns the String value of the subsequence from the start index to the end index.

Parameters
start int: the inclusive start index to begin the subsequence.
end int: the exclusive end index to end the subsequence.
Returns
String a String containing the subsequence.

public String toString ()

Added in API level 1

Returns the current String representation.

Returns
String a String containing the characters in this instance.

public void trimToSize ()

Added in API level 1

Trims off any extra capacity beyond the current length. Note, this method is NOT guaranteed to change the capacity of this object.