java.lang.Object | |
↳ | java.lang.StringBuffer |
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()
.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new StringBuffer using the default capacity which is 16.
| |||||||||||
Constructs a new StringBuffer using the specified capacity.
| |||||||||||
Constructs a new StringBuffer containing the characters in the specified
string.
| |||||||||||
Constructs a StringBuffer and initializes it with the content from the
specified
CharSequence . |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds the specified sequence of characters to the end of this buffer.
| |||||||||||
Adds the string representation of the specified double to the end of this
StringBuffer.
| |||||||||||
Adds the string representation of the specified boolean to the end of
this StringBuffer.
| |||||||||||
Adds the string representation of the specified long to the end of this
StringBuffer.
| |||||||||||
Adds the string representation of the specified float to the end of this
StringBuffer.
| |||||||||||
Adds the character array to the end of this buffer.
| |||||||||||
Adds the string representation of the specified integer to the end of
this StringBuffer.
| |||||||||||
Adds the specified StringBuffer to the end of this buffer.
| |||||||||||
Appends the specified CharSequence to this buffer.
| |||||||||||
Adds the specified character to the end of this buffer.
| |||||||||||
Adds the specified string to the end of this buffer.
| |||||||||||
Appends the specified subsequence of the CharSequence to this buffer.
| |||||||||||
Adds the string representation of the specified object to the end of this
StringBuffer.
| |||||||||||
Appends the string representation of the specified Unicode code point to
the end of this buffer.
| |||||||||||
Returns the number of characters that can be held without growing.
| |||||||||||
Retrieves the character at the
index . | |||||||||||
Retrieves the Unicode code point value at the
index . | |||||||||||
Retrieves the Unicode code point value that precedes the
index . | |||||||||||
Calculates the number of Unicode code points between
start
and end . | |||||||||||
Deletes a range of characters.
| |||||||||||
Deletes the character at the specified offset.
| |||||||||||
Ensures that this object has a minimum capacity available before
requiring the internal buffer to be enlarged.
| |||||||||||
Copies the requested sequence of characters to the
char[] passed
starting at idx . | |||||||||||
Searches for the index of the specified character.
| |||||||||||
Searches for the first index of the specified character.
| |||||||||||
Inserts the specified subsequence into this buffer at the specified
index.
| |||||||||||
Inserts the string representation of the specified integer into this
buffer at the specified offset.
| |||||||||||
Inserts the string representation of the specified into this buffer
double at the specified offset.
| |||||||||||
Inserts the specified subsequence of characters into this buffer at the
specified index.
| |||||||||||
Inserts the string into this buffer at the specified offset.
| |||||||||||
Inserts the string representation of the specified long into this buffer
at the specified offset.
| |||||||||||
Inserts the string representation of the specified object into this
buffer at the specified offset.
| |||||||||||
Inserts the string representation of the specified float into this buffer
at the specified offset.
| |||||||||||
Inserts the character into this buffer at the specified offset.
| |||||||||||
Inserts the character array into this buffer at the specified offset.
| |||||||||||
Inserts the specified CharSequence into this buffer at the specified
index.
| |||||||||||
Inserts the string representation of the specified boolean into this
buffer at the specified offset.
| |||||||||||
Searches for the last index of the specified character.
| |||||||||||
Searches for the index of the specified character.
| |||||||||||
The current length.
| |||||||||||
Returns the index that is offset
codePointOffset code points from
index . | |||||||||||
Replaces the characters in the specified range with the contents of the
specified string.
| |||||||||||
Reverses the order of characters in this buffer.
| |||||||||||
Sets the character at the
index . | |||||||||||
Sets the current length to a new value.
| |||||||||||
Returns a
CharSequence of the subsequence from the start
index to the end index. | |||||||||||
Returns the String value of the subsequence from the
start index
to the current end. | |||||||||||
Returns the String value of the subsequence from the
start index
to the end index. | |||||||||||
Returns the current String representation.
| |||||||||||
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
|
Constructs a new StringBuffer using the default capacity which is 16.
Constructs a new StringBuffer using the specified capacity.
capacity | the initial capacity. |
---|
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.
string | the string content with which to initialize the new instance. |
---|
NullPointerException | if string is null .
|
---|
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.
cs | the content to initialize the instance. |
---|
NullPointerException | if cs is null . |
---|
Adds the specified sequence of characters to the end of this buffer.
chars | the character array to append. |
---|---|
start | the starting offset. |
length | the number of characters. |
ArrayIndexOutOfBoundsException | if length < 0 , start < 0 or start +
length > chars.length . |
---|---|
NullPointerException | if chars is null .
|
Adds the string representation of the specified double to the end of this StringBuffer.
d | the double to append. |
---|
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.
b | the boolean to append. |
---|
Adds the string representation of the specified long to the end of this StringBuffer.
l | the long to append. |
---|
Adds the string representation of the specified float to the end of this StringBuffer.
f | the float to append. |
---|
Adds the character array to the end of this buffer.
chars | the character array to append. |
---|
NullPointerException | if chars is null .
|
---|
Adds the string representation of the specified integer to the end of this StringBuffer.
i | the integer to append. |
---|
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.
sb | the StringBuffer to append (may be null). |
---|
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.
s | the CharSequence to append. |
---|
Adds the specified character to the end of this buffer.
ch | the character to append. |
---|
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.
string | the string to append (may be null). |
---|
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.
s | the CharSequence to append. |
---|---|
start | the inclusive start index. |
end | the exclusive end index. |
IndexOutOfBoundsException | if start or end are negative, start
is greater than end or end is greater than
the length of s . |
---|
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.
obj | the object to append (may be null). |
---|
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)
.
codePoint | the Unicode code point to encode and append. |
---|
Returns the number of characters that can be held without growing.
Retrieves the character at the index
.
index | the index of the character to retrieve. |
---|
Retrieves the Unicode code point value at the index
.
index | the index to the char code unit. |
---|
Retrieves the Unicode code point value that precedes the index
.
index | the index to the char code unit within this object. |
---|
Calculates the number of Unicode code points between start
and end
.
beginIndex | the inclusive beginning index of the subsequence. |
---|---|
endIndex | the exclusive end index of the subsequence. |
Deletes a range of characters.
start | the offset of the first character. |
---|---|
end | the offset one past the last character. |
StringIndexOutOfBoundsException | if start < 0 , start > end or end >
length() .
|
---|
Deletes the character at the specified offset.
location | the offset of the character to delete. |
---|
StringIndexOutOfBoundsException | if location < 0 or location >= length()
|
---|
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.
min | the new minimum capacity to set. |
---|
Copies the requested sequence of characters to the char[]
passed
starting at idx
.
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. |
IndexOutOfBoundsException | if start < 0 , end > length() , start >
end , index < 0 , end - start > buffer.length -
index
|
---|
Searches for the index of the specified character. The search for the character starts at the specified offset and moves towards the end.
subString | the string to find. |
---|---|
start | the starting offset. |
Searches for the first index of the specified character. The search for the character starts at the beginning and moves towards the end.
string | the string to find. |
---|
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.
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. |
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 . |
---|
Inserts the string representation of the specified integer into this buffer at the specified offset.
index | the index at which to insert. |
---|---|
i | the integer to insert. |
StringIndexOutOfBoundsException | if index < 0 or index > length() .
|
---|
Inserts the string representation of the specified into this buffer double at the specified offset.
index | the index at which to insert. |
---|---|
d | the double to insert. |
StringIndexOutOfBoundsException | if index < 0 or index > length() .
|
---|
Inserts the specified subsequence of characters into this buffer at the specified index.
index | the index at which to insert. |
---|---|
chars | the character array to insert. |
start | the starting offset. |
length | the number of characters. |
NullPointerException | if chars is null . |
---|---|
StringIndexOutOfBoundsException | if length < 0 , start < 0 , start +
length > chars.length , index < 0 or index >
length()
|
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.
index | the index at which to insert. |
---|---|
string | the string to insert (may be null). |
StringIndexOutOfBoundsException | if index < 0 or index > length() .
|
---|
Inserts the string representation of the specified long into this buffer at the specified offset.
index | the index at which to insert. |
---|---|
l | the long to insert. |
StringIndexOutOfBoundsException | if index < 0 or index > length() .
|
---|
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.
index | the index at which to insert. |
---|---|
obj | the object to insert (may be null). |
StringIndexOutOfBoundsException | if index < 0 or index > length() .
|
---|
Inserts the string representation of the specified float into this buffer at the specified offset.
index | the index at which to insert. |
---|---|
f | the float to insert. |
StringIndexOutOfBoundsException | if index < 0 or index > length() .
|
---|
Inserts the character into this buffer at the specified offset.
index | the index at which to insert. |
---|---|
ch | the character to insert. |
ArrayIndexOutOfBoundsException | if index < 0 or index > length() .
|
---|
Inserts the character array into this buffer at the specified offset.
index | the index at which to insert. |
---|---|
chars | the character array to insert. |
StringIndexOutOfBoundsException | if index < 0 or index > length() . |
---|---|
NullPointerException | if chars is null .
|
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.
index | The index at which to insert. |
---|---|
s | The char sequence to insert. |
IndexOutOfBoundsException | if index < 0 or index > length() . |
---|
Inserts the string representation of the specified boolean into this buffer at the specified offset.
index | the index at which to insert. |
---|---|
b | the boolean to insert. |
StringIndexOutOfBoundsException | if index < 0 or index > length() .
|
---|
Searches for the last index of the specified character. The search for the character starts at the end and moves towards the beginning.
string | the string to find. |
---|
NullPointerException | if string is null . |
---|
Searches for the index of the specified character. The search for the character starts at the specified offset and moves towards the beginning.
subString | the string to find. |
---|---|
start | the starting offset. |
The current length.
Returns the index that is offset codePointOffset
code points from
index
.
index | the index to calculate the offset from. |
---|---|
codePointOffset | the number of code points to count. |
codePointOffset
code points away from
index.Replaces the characters in the specified range with the contents of the specified string.
start | the inclusive begin index. |
---|---|
end | the exclusive end index. |
string | the string that will replace the contents in the range. |
StringIndexOutOfBoundsException | if start or end are negative, start
is greater than end or end is greater than
the length of s .
|
---|
Reverses the order of characters in this buffer.
Sets the character at the index
.
index | the zero-based index of the character to replace. |
---|---|
ch | the character to set. |
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