java.lang
public
final
class
java.lang.StringBuffer
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
Public Methods
|
|
|
|
|
StringBuffer |
append(double d) |
|
synchronized |
|
|
|
StringBuffer |
append(CharSequence s) |
|
|
|
|
|
StringBuffer |
append(long l) |
|
|
|
|
|
StringBuffer |
append(int i) |
|
|
|
|
|
StringBuffer |
append(boolean b) |
|
synchronized |
|
|
|
StringBuffer |
append(Object obj) |
|
synchronized |
|
|
|
StringBuffer |
append(char ch) |
|
synchronized |
|
|
|
StringBuffer |
append(char[] chars) |
|
synchronized |
|
|
|
StringBuffer |
append(String string) |
|
|
|
|
|
StringBuffer |
append(float f) |
|
synchronized |
|
|
|
StringBuffer |
append(char[] chars, int start, int length) |
|
synchronized |
|
|
|
StringBuffer |
append(CharSequence s, int start, int end) |
|
synchronized |
|
|
|
StringBuffer |
append(StringBuffer sb) |
|
|
|
|
|
StringBuffer |
appendCodePoint(int codePoint) |
|
|
|
|
|
int |
capacity() |
|
synchronized |
|
|
|
char |
charAt(int 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) |
|
synchronized |
|
|
|
StringBuffer |
deleteCharAt(int location) |
|
synchronized |
|
|
|
void |
ensureCapacity(int min) |
|
synchronized |
|
|
|
void |
getChars(int start, int end, char[] buffer, int idx) |
|
synchronized |
|
|
|
int |
indexOf(String subString, int start) |
|
|
|
|
|
int |
indexOf(String string) |
|
|
|
|
|
StringBuffer |
insert(int index, Object obj) |
|
synchronized |
|
|
|
StringBuffer |
insert(int index, String string) |
|
synchronized |
|
|
|
StringBuffer |
insert(int index, char[] chars) |
|
|
|
|
|
StringBuffer |
insert(int index, boolean b) |
|
|
|
|
|
StringBuffer |
insert(int index, double d) |
|
synchronized |
|
|
|
StringBuffer |
insert(int index, char ch) |
|
|
|
|
|
StringBuffer |
insert(int index, float f) |
|
|
|
|
|
StringBuffer |
insert(int index, int i) |
|
synchronized |
|
|
|
StringBuffer |
insert(int index, CharSequence s) |
|
synchronized |
|
|
|
StringBuffer |
insert(int index, CharSequence s, int start, int end) |
|
synchronized |
|
|
|
StringBuffer |
insert(int index, char[] chars, int start, int length) |
|
|
|
|
|
StringBuffer |
insert(int index, long l) |
|
|
|
|
|
int |
lastIndexOf(String string) |
|
synchronized |
|
|
|
int |
lastIndexOf(String subString, int start) |
|
|
|
|
|
int |
length() |
|
synchronized |
|
|
|
int |
offsetByCodePoints(int index, int codePointOffset) |
|
synchronized |
|
|
|
StringBuffer |
replace(int start, int end, String string) |
|
synchronized |
|
|
|
StringBuffer |
reverse() |
|
synchronized |
|
|
|
void |
setCharAt(int index, char ch) |
|
synchronized |
|
|
|
void |
setLength(int length) |
|
synchronized |
|
|
|
CharSequence |
subSequence(int start, int end) |
|
synchronized |
|
|
|
String |
substring(int start, int end) |
|
synchronized |
|
|
|
String |
substring(int start) |
|
synchronized |
|
|
|
String |
toString() |
|
synchronized |
|
|
|
void |
trimToSize() |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
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 |
Constructs a StringBuffer and initializes it with the characters in the
CharSequence
.
Parameters
cs
| The CharSequence to initialize the instance. |
Public Methods
Adds the string representation of the specified double to the end of this
StringBuffer.
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.
Adds the string representation of the specified long to the end of this
StringBuffer.
Adds the string representation of the specified integer to the end of
this StringBuffer.
public
StringBuffer
append(boolean b)
Adds the string representation of the specified boolean to the end of
this StringBuffer.
Adds the string representation of the specified object to the end of this
StringBuffer.
public
synchronized
StringBuffer
append(char ch)
Adds the specified character to the end of this StringBuffer.
public
synchronized
StringBuffer
append(char[] chars)
Adds the character array to the end of this StringBuffer.
Parameters
chars
| the character array |
Adds the specified string to the end of this StringBuffer.
Adds the string representation of the specified float to the end of 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 |
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.
Adds the specified StringBuffer to the end of 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.
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
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.
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.
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.
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 |
public
synchronized
StringBuffer
deleteCharAt(int location)
Deletes a single character
Parameters
location
| the offset of the character to delete |
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 |
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
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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.
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 |
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 |
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
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.
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 |
public
synchronized
StringBuffer
reverse()
Reverses the order of characters in 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 |
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 |
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
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
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
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.