StringBuffer - JRE Emulation | JRE Emulation
public final class

StringBuffer

extends Object
implements Appendable CharSequence Serializable
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(boolean b)
Adds the string representation of the specified boolean to the end of this StringBuffer.
StringBuffer append(long l)
Adds the string representation of the specified long to the end of this StringBuffer.
synchronized StringBuffer append(char ch)
Adds the specified character to the end of this buffer.
synchronized StringBuffer append(Object obj)
Adds the string representation of the specified object to the end of this StringBuffer.
synchronized 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.
synchronized StringBuffer append(char[] chars)
Adds the character array to the end of this buffer.
synchronized StringBuffer append(String string)
Adds the specified string to the end of this buffer.
synchronized StringBuffer append(StringBuffer sb)
Adds the specified StringBuffer to the end of this buffer.
StringBuffer append(float f)
Adds the string representation of the specified float to the end of this StringBuffer.
StringBuffer append(int i)
Adds the string representation of the specified integer to the end of this StringBuffer.
synchronized StringBuffer append(CharSequence s, int start, int end)
Appends the specified subsequence of the CharSequence to this buffer.
synchronized 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()
synchronized char charAt(int index)
Returns the char value at the specified index.
synchronized int codePointAt(int index)
synchronized int codePointBefore(int index)
synchronized int codePointCount(int beginIndex, int endIndex)
synchronized StringBuffer delete(int start, int end)
Deletes a range of characters.
synchronized StringBuffer deleteCharAt(int location)
Deletes the character at the specified offset.
synchronized void ensureCapacity(int min)
synchronized 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 s)
synchronized int indexOf(String subString, int start)
synchronized StringBuffer insert(int index, char[] chars)
Inserts the character array 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.
synchronized StringBuffer insert(int index, CharSequence s)
Inserts the specified CharSequence into this buffer at the specified index.
synchronized StringBuffer insert(int index, char ch)
Inserts the character 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.
synchronized 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, int i)
Inserts the string representation of the specified integer into this buffer at the specified offset.
synchronized StringBuffer insert(int index, String string)
Inserts the string 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.
synchronized 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, boolean b)
Inserts the string representation of the specified boolean into this buffer at the specified offset.
synchronized int lastIndexOf(String subString, int start)
int lastIndexOf(String s)
int length()
Returns the length of this character sequence.
synchronized int offsetByCodePoints(int index, int codePointOffset)
synchronized StringBuffer replace(int start, int end, String string)
Replaces the characters in the specified range with the contents of the specified string.
synchronized StringBuffer reverse()
Reverses the order of characters in this buffer.
synchronized void setCharAt(int index, char ch)
synchronized void setLength(int length)
synchronized CharSequence subSequence(int start, int end)
Returns a new CharSequence that is a subsequence of this sequence.
synchronized String substring(int start, int end)
synchronized String substring(int start)
synchronized String toString()
Returns a string containing a concise, human-readable description of this object.
synchronized void trimToSize()
Inherited Methods
[Expand]
From class java.lang.Object
From interface java.lang.Appendable
From interface java.lang.CharSequence

Public Constructors

public StringBuffer ()

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

public StringBuffer (int capacity)

Constructs a new StringBuffer using the specified capacity.

Parameters
capacity the initial capacity.

public StringBuffer (String string)

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 the string content with which to initialize the new instance.
Throws
NullPointerException if string is null.

public StringBuffer (CharSequence cs)

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 the content to initialize the instance.
Throws
NullPointerException if cs is null.

Public Methods

public StringBuffer append (boolean b)

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 the boolean to append.
Returns
  • this StringBuffer.
See Also

public StringBuffer append (long l)

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

Parameters
l the long to append.
Returns
  • this StringBuffer.
See Also

public synchronized StringBuffer append (char ch)

Adds the specified character to the end of this buffer.

Parameters
ch the character to append.
Returns
  • this StringBuffer.
See Also

public synchronized StringBuffer append (Object obj)

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 the object to append (may be null).
Returns
  • this StringBuffer.
See Also

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

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

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

public StringBuffer append (double d)

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

Parameters
d the double to append.
Returns
  • this StringBuffer.
See Also

public synchronized StringBuffer append (char[] chars)

Adds the character array to the end of this buffer.

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

public synchronized StringBuffer append (String string)

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 the string to append (may be null).
Returns
  • this StringBuffer.

public synchronized StringBuffer append (StringBuffer sb)

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 the StringBuffer to append (may be null).
Returns
  • this StringBuffer.

public StringBuffer append (float f)

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

Parameters
f the float to append.
Returns
  • this StringBuffer.
See Also

public StringBuffer append (int i)

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

Parameters
i the integer to append.
Returns
  • this StringBuffer.
See Also

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

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 the CharSequence to append.
start the inclusive start index.
end the exclusive end index.
Returns
  • 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 synchronized StringBuffer append (CharSequence s)

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 the CharSequence to append.
Returns
  • this StringBuffer.

public StringBuffer appendCodePoint (int codePoint)

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 the Unicode code point to encode and append.
Returns
  • this StringBuffer.
See Also

public int capacity ()

public synchronized char charAt (int index)

Returns the char value at the specified index. An index ranges from zero to length() - 1. The first char value of the sequence is at index zero, the next at index one, and so on, as for array indexing.

If the char value specified by the index is a surrogate, the surrogate value is returned.

Parameters
index the index of the char value to be returned
Returns
  • the specified char value

public synchronized int codePointAt (int index)

public synchronized int codePointBefore (int index)

public synchronized int codePointCount (int beginIndex, int endIndex)

public synchronized StringBuffer delete (int start, int end)

Deletes a range of characters.

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

public synchronized StringBuffer deleteCharAt (int location)

Deletes the character at the specified offset.

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

public synchronized void ensureCapacity (int min)

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

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

Parameters
start the starting offset of characters to copy.
end the ending offset of characters to copy.
buffer the destination character array.
idx 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 s)

public synchronized int indexOf (String subString, int start)

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

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

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

public StringBuffer insert (int index, float f)

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

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

public synchronized StringBuffer insert (int index, CharSequence s)

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 The index at which to insert.
s The char sequence to insert.
Returns
  • this buffer.
Throws
IndexOutOfBoundsException if index < 0 or index > length().

public synchronized StringBuffer insert (int index, char ch)

Inserts the character into this buffer at the specified offset.

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

public StringBuffer insert (int index, long l)

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

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

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

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

Parameters
index the index at which to insert.
chars the character array to insert.
start the starting offset.
length the number of characters.
Returns
  • 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, int i)

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

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

public synchronized StringBuffer insert (int index, String string)

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 the index at which to insert.
string the string to insert (may be null).
Returns
  • this buffer.
Throws
StringIndexOutOfBoundsException if index < 0 or index > length().

public StringBuffer insert (int index, double d)

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

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

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

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 The index at which to insert.
s The char sequence to insert.
start The inclusive start index in the char sequence.
end The exclusive end index in the char sequence.
Returns
  • 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)

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 the index at which to insert.
obj the object to insert (may be null).
Returns
  • this buffer.
Throws
StringIndexOutOfBoundsException if index < 0 or index > length().

public StringBuffer insert (int index, boolean b)

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

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

public synchronized int lastIndexOf (String subString, int start)

public int lastIndexOf (String s)

public int length ()

Returns the length of this character sequence. The length is the number of 16-bit chars in the sequence.

Returns
  • the number of chars in this sequence

public synchronized int offsetByCodePoints (int index, int codePointOffset)

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

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

Parameters
start the inclusive begin index.
end the exclusive end index.
string the string that will replace the contents in the range.
Returns
  • 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 synchronized StringBuffer reverse ()

Reverses the order of characters in this buffer.

Returns
  • this buffer.

public synchronized void setCharAt (int index, char ch)

public synchronized void setLength (int length)

public synchronized CharSequence subSequence (int start, int end)

Returns a new CharSequence that is a subsequence of this sequence. The subsequence starts with the char value at the specified index and ends with the char value at index end - 1. The length (in chars) of the returned sequence is end - start, so if start == end then an empty sequence is returned.

Parameters
start the start index, inclusive
end the end index, exclusive
Returns
  • the specified subsequence

public synchronized String substring (int start, int end)

public synchronized String substring (int start)

public synchronized String toString ()

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
  • a printable representation of this object.

public synchronized void trimToSize ()