加载中...
The Wayback Machine - https://sup1a9wrlpyh5li9ro.vcoronado.top/web/20090226002841/http://developer.android.com:80/reference/java/lang/StringBuffer.html
public final class

StringBuffer

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

Class Overview

StringBuffer is a variable size contiguous indexable array of characters. The length of the StringBuffer is the number of characters it contains. The capacity of the StringBuffer is the number of characters it can hold.

Characters may be inserted at any position up to the length of the StringBuffer, increasing the length of the StringBuffer. Characters at any position in the StringBuffer may be replaced, which does not affect the StringBuffer length.

The capacity of a StringBuffer may be specified when the StringBuffer is created. If the capacity of the StringBuffer is exceeded, the capacity is increased.

Summary

Public Constructors
StringBuffer()
Constructs a new StringBuffer using the default capacity.
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 and the default capacity.
StringBuffer(CharSequence cs)

Constructs a StringBuffer and initializes it with the characters in the CharSequence.

Public Methods
StringBuffer append(double d)
Adds the string representation of the specified double to the end of this StringBuffer.
synchronized StringBuffer append(CharSequence s)

Appends the CharSequence to this buffer.

StringBuffer append(long l)
Adds the string representation of the specified long to the end of this StringBuffer.
StringBuffer append(int i)
Adds the string representation of the specified integer to the end of this StringBuffer.
StringBuffer append(boolean b)
Adds the string representation of the specified boolean to the end of this StringBuffer.
synchronized StringBuffer append(Object obj)
Adds the string representation of the specified object to the end of this StringBuffer.
synchronized StringBuffer append(char ch)
Adds the specified character to the end of this StringBuffer.
synchronized StringBuffer append(char[] chars)
Adds the character array to the end of this StringBuffer.
synchronized StringBuffer append(String string)
Adds the specified string to the end of this StringBuffer.
StringBuffer append(float f)
Adds the string representation of the specified float 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 StringBuffer.
synchronized StringBuffer append(CharSequence s, int start, int end)

Appends the subsequence of the CharSequence to this buffer.

synchronized StringBuffer append(StringBuffer sb)
Adds the specified StringBuffer to the end of this StringBuffer.
StringBuffer appendCodePoint(int codePoint)

Appends the encoded Unicode code point to this object.

int capacity()
Returns the number of characters this StringBuffer can hold without growing.
synchronized char charAt(int index)
Returns the character at the specified offset in this StringBuffer.
synchronized int codePointAt(int index)

Retrieves the Unicode code point value at the index.

synchronized int codePointBefore(int index)

Retrieves the Unicode code point value that precedes the index.

synchronized int codePointCount(int beginIndex, int endIndex)

Calculates the number of Unicode code points between beginIndex and endIndex.

synchronized StringBuffer delete(int start, int end)
Deletes a range of characters.
synchronized StringBuffer deleteCharAt(int location)
Deletes a single character
synchronized void ensureCapacity(int min)
Ensures that this StringBuffer can hold the specified number of characters without growing.
synchronized void getChars(int start, int end, char[] buffer, int idx)
Copies the specified characters in this StringBuffer to the character array starting at the specified offset in the character array.
synchronized int indexOf(String subString, int start)
Searches in this StringBuffer for the index of the specified character.
int indexOf(String string)
Searches in this StringBuffer for the first index of the specified character.
StringBuffer insert(int index, Object obj)
Inserts the string representation of the specified object at the specified offset in this StringBuffer.
synchronized StringBuffer insert(int index, String string)
Inserts the string at the specified offset in this StringBuffer.
synchronized StringBuffer insert(int index, char[] chars)
Inserts the character array at the specified offset in this StringBuffer.
StringBuffer insert(int index, boolean b)
Inserts the string representation of the specified boolean at the specified offset in this StringBuffer.
StringBuffer insert(int index, double d)
Inserts the string representation of the specified double at the specified offset in this StringBuffer.
synchronized StringBuffer insert(int index, char ch)
Inserts the character at the specified offset in this StringBuffer.
StringBuffer insert(int index, float f)
Inserts the string representation of the specified float at the specified offset in this StringBuffer.
StringBuffer insert(int index, int i)
Inserts the string representation of the specified integer at the specified offset in this StringBuffer.
synchronized StringBuffer insert(int index, CharSequence s)

Inserts the CharSequence into this buffer at the index.

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

Inserts the CharSequence into this buffer at the index.

synchronized StringBuffer insert(int index, char[] chars, int start, int length)
Inserts the specified sequence of characters at the specified offset in this StringBuffer.
StringBuffer insert(int index, long l)
Inserts the string representation of the specified long at the specified offset in this StringBuffer.
int lastIndexOf(String string)
Searches in this StringBuffer for the last index of the specified character.
synchronized int lastIndexOf(String subString, int start)
Searches in this StringBuffer for the index of the specified character.
int length()

The current length of this object.

synchronized int offsetByCodePoints(int index, int codePointOffset)

Returns the index within this object that is offset from index by codePointOffset code points.

synchronized StringBuffer replace(int start, int end, String string)
Replace a range of characters with the characters in the specified String.
synchronized StringBuffer reverse()
Reverses the order of characters in this StringBuffer.
synchronized void setCharAt(int index, char ch)
Sets the character at the specified offset in this StringBuffer.
synchronized void setLength(int length)
Sets the length of this StringBuffer to the specified length.
synchronized CharSequence subSequence(int start, int end)
Copies a range of characters into a new String.
synchronized String substring(int start, int end)
Copies a range of characters into a new String.
synchronized String substring(int start)
Copies a range of characters into a new String.
synchronized String toString()
Returns the contents of this StringBuffer.
synchronized void trimToSize()

Trims the storage capacity of this buffer down to the size of the current character sequence.

[Expand]
Inherited Methods
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.

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 and the default capacity.

Parameters
string the string content with which to initialize the new StringBuffer instance
Throws
NullPointerException on supplying a null value of string

public StringBuffer (CharSequence cs)

Constructs a StringBuffer and initializes it with the characters in the CharSequence.

Parameters
cs The CharSequence to initialize the instance.
Throws
NullPointerException if the cs parameter is null.

Public Methods

public StringBuffer append (double d)

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

Parameters
d the double
Returns
  • this StringBuffer

public synchronized StringBuffer append (CharSequence s)

Appends the CharSequence to this buffer. If the CharSequence is null, then the string "null" is appended.

Parameters
s The CharSequence to append.
Returns
  • A reference to this object.

public StringBuffer append (long l)

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

Parameters
l the long
Returns
  • this StringBuffer

public StringBuffer append (int i)

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

Parameters
i the integer
Returns
  • this StringBuffer

public StringBuffer append (boolean b)

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

Parameters
b the boolean
Returns
  • this StringBuffer

public synchronized StringBuffer append (Object obj)

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

Parameters
obj the object
Returns
  • this StringBuffer

public synchronized StringBuffer append (char ch)

Adds the specified character to the end of this StringBuffer.

Parameters
ch a character
Returns
  • this StringBuffer

public synchronized StringBuffer append (char[] chars)

Adds the character array to the end of this StringBuffer.

Parameters
chars the character array
Returns
  • this StringBuffer
Throws
NullPointerException when chars is null

public synchronized StringBuffer append (String string)

Adds the specified string to the end of this StringBuffer.

Parameters
string the string
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
Returns
  • this StringBuffer

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

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

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

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

Appends the subsequence of the CharSequence to this buffer. If the CharSequence is null, then the string "null" is used to extract a subsequence.

Parameters
s The CharSequence to append.
start The inclusive start index of the subsequence of the CharSequence.
end The exclusive end index of the subsequence of the CharSequence.
Returns
  • A reference to this object.
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 (StringBuffer sb)

Adds the specified StringBuffer to the end of this StringBuffer.

Parameters
sb the StringBuffer
Returns
  • this StringBuffer

public StringBuffer appendCodePoint (int codePoint)

Appends the encoded Unicode code point to this object. The code point is converted to a char[] as defined by toChars(int).

Parameters
codePoint The Unicode code point to encode and append.
Returns
  • A reference to this object.
See Also

public int capacity ()

Returns the number of characters this StringBuffer can hold without growing.

Returns
  • the capacity of this StringBuffer

public synchronized char charAt (int index)

Returns the character at the specified offset in this StringBuffer.

Parameters
index the zero-based index in this StringBuffer
Returns
  • the character at the index
Throws
IndexOutOfBoundsException when index < 0 or index >= length()

public synchronized int codePointAt (int index)

Retrieves the Unicode code point value at the index.

Parameters
index The index to the char code unit within this object.
Returns
  • The Unicode code point value.
Throws
IndexOutOfBoundsException if index is negative or greater than or equal to length().

public synchronized int codePointBefore (int index)

Retrieves the Unicode code point value that precedes the index.

Parameters
index The index to the char code unit within this object.
Returns
  • The Unicode code point value.
Throws
IndexOutOfBoundsException if index is less than 1 or greater than length().

public synchronized int codePointCount (int beginIndex, int endIndex)

Calculates the number of Unicode code points between beginIndex and endIndex.

Parameters
beginIndex The inclusive beginning index of the subsequence.
endIndex The exclusive end index of the subsequence.
Returns
  • The number of Unicode code points in the subsequence.
Throws
IndexOutOfBoundsException if beginIndex is negative or greater than endIndex or endIndex is greater than length().

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 when start < 0, start > end or end > length()

public synchronized StringBuffer deleteCharAt (int location)

Deletes a single character

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

public synchronized void ensureCapacity (int min)

Ensures that this StringBuffer can hold the specified number of characters without growing.

Parameters
min the minimum number of elements that this StringBuffer will hold before growing

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

Copies the specified characters in this StringBuffer to the character array starting at the specified offset in the character array.

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 when start < 0, end > length(), start > end, index < 0, end - start > buffer.length - index
NullPointerException when buffer is null

public synchronized int indexOf (String subString, int start)

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

Parameters
subString the string to find
start the starting offset
Returns
  • the index in this StringBuffer of the specified character, -1 if the character isn't found

public int indexOf (String string)

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

Parameters
string the string to find
Returns
  • the index in this StringBuffer of the specified character, -1 if the character isn't found

public StringBuffer insert (int index, Object obj)

Inserts the string representation of the specified object at the specified offset in this StringBuffer.

Parameters
index the index at which to insert
obj the object to insert
Returns
  • this StringBuffer
Throws
StringIndexOutOfBoundsException when index < 0 or index > length()

public synchronized StringBuffer insert (int index, String string)

Inserts the string at the specified offset in this StringBuffer.

Parameters
index the index at which to insert
string the string to insert
Returns
  • this StringBuffer
Throws
StringIndexOutOfBoundsException when index < 0 or index > length()

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

Inserts the character array at the specified offset in this StringBuffer.

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

public StringBuffer insert (int index, boolean b)

Inserts the string representation of the specified boolean at the specified offset in this StringBuffer.

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

public StringBuffer insert (int index, double d)

Inserts the string representation of the specified double at the specified offset in this StringBuffer.

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

public synchronized StringBuffer insert (int index, char ch)

Inserts the character at the specified offset in this StringBuffer.

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

public StringBuffer insert (int index, float f)

Inserts the string representation of the specified float at the specified offset in this StringBuffer.

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

public StringBuffer insert (int index, int i)

Inserts the string representation of the specified integer at the specified offset in this StringBuffer.

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

public synchronized StringBuffer insert (int index, CharSequence s)

Inserts the CharSequence into this buffer at the index. If CharSequence is null, then the string "null" is inserted.

Parameters
index The index of this buffer to insert the sequence.
s The CharSequence to insert.
Returns
  • A reference to this object.
Throws
IndexOutOfBoundsException if the index is invalid.

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

Inserts the CharSequence into this buffer at the index. If CharSequence is null, then the string "null" is inserted.

Parameters
index The index of this buffer to insert the sequence.
s The CharSequence to insert.
start The inclusive start index of the subsequence of the CharSequence.
end The exclusive end index of the subsequence of the CharSequence.
Returns
  • A reference to this object.
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 synchronized StringBuffer insert (int index, char[] chars, int start, int length)

Inserts the specified sequence of characters at the specified offset in this StringBuffer.

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

public StringBuffer insert (int index, long l)

Inserts the string representation of the specified long at the specified offset in this StringBuffer.

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

public int lastIndexOf (String string)

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

Parameters
string the string to find
Returns
  • the index in this StringBuffer of the specified character, -1 if the character isn't found
Throws
NullPointerException if the string parameter is null.

public synchronized int lastIndexOf (String subString, int start)

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

Parameters
subString the string to find
start the starting offset
Returns
  • the index in this StringBuffer of the specified character, -1 if the character isn't found

public int length ()

The current length of this object.

Returns
  • the number of characters in this StringBuffer

public synchronized int offsetByCodePoints (int index, int codePointOffset)

Returns the index within this object that is offset from index by codePointOffset code points.

Parameters
index The index within this object to calculate the offset from.
codePointOffset The number of code points to count.
Returns
  • The index within this object that is the offset.
Throws
IndexOutOfBoundsException if index is negative or greater than length() or if there aren't enough code points before or after index to match codePointOffset.

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

Replace a range of characters with the characters in the specified String.

Parameters
start the offset of the first character
end the offset one past the last character
string a String
Returns
  • this StringBuffer
Throws
StringIndexOutOfBoundsException when start < 0 or start > end

public synchronized StringBuffer reverse ()

Reverses the order of characters in this StringBuffer.

Returns
  • this StringBuffer

public synchronized void setCharAt (int index, char ch)

Sets the character at the specified offset in this StringBuffer.

Parameters
index the zero-based index in this StringBuffer
ch the character
Throws
IndexOutOfBoundsException when index < 0 or index >= length()

public synchronized void setLength (int length)

Sets the length of this StringBuffer to the specified length. If there are more than length characters in this StringBuffer, the characters at end are lost. If there are less than length characters in the StringBuffer, the additional characters are set to \\u0000.

Parameters
length the new length of this StringBuffer
Throws
IndexOutOfBoundsException when length < 0
See Also

public synchronized CharSequence subSequence (int start, int end)

Copies a range of characters into a new String.

Parameters
start the offset of the first character
end the offset one past the last character
Returns
  • a new String containing the characters from start to end - 1
Throws
IndexOutOfBoundsException when start < 0, start > end or end > length()

public synchronized String substring (int start, int end)

Copies a range of characters into a new String.

Parameters
start the offset of the first character
end the offset one past the last character
Returns
  • a new String containing the characters from start to end - 1
Throws
StringIndexOutOfBoundsException when start < 0, start > end or end > length()

public synchronized String substring (int start)

Copies a range of characters into a new String.

Parameters
start the offset of the first character
Returns
  • a new String containing the characters from start to the end of the string
Throws
StringIndexOutOfBoundsException when start < 0 or start > length()

public synchronized String toString ()

Returns the contents of this StringBuffer.

Returns
  • a String containing the characters in this StringBuffer

public synchronized void trimToSize ()

Trims the storage capacity of this buffer down to the size of the current character sequence. Execution of this method may change the results returned by the capacity() method, but this is not required.