public final class StringBuffer extends Object implements Serializable, CharSequence, Appendable
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.
String
,
Serialized FormConstructor and Description |
---|
StringBuffer()
Constructs a new StringBuffer using the default capacity.
|
StringBuffer(CharSequence sequence)
Constructs a new StringBuffer containing the characters in
the specified CharSequence and 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.
|
Modifier and Type | Method and Description |
---|---|
StringBuffer |
append(boolean value)
Adds the string representation of the specified boolean to the
end of this StringBuffer.
|
StringBuffer |
append(char ch)
Adds the specified character to the end of
this StringBuffer.
|
StringBuffer |
append(char[] chars)
Adds the character array to the end of this StringBuffer.
|
StringBuffer |
append(char[] chars,
int start,
int length)
Adds the specified sequence of characters to the end of
this StringBuffer.
|
StringBuffer |
append(CharSequence sequence)
Adds the specified CharSequence to the end of this StringBuffer.
|
StringBuffer |
append(CharSequence sequence,
int start,
int end)
Adds the specified CharSequence to the end of this StringBuffer.
|
StringBuffer |
append(double value)
Adds the string representation of the specified double to the
end of this StringBuffer.
|
StringBuffer |
append(float value)
Adds the string representation of the specified float to the
end of this StringBuffer.
|
StringBuffer |
append(int value)
Adds the string representation of the specified integer to the
end of this StringBuffer.
|
StringBuffer |
append(long value)
Adds the string representation of the specified long to the
end of this StringBuffer.
|
StringBuffer |
append(Object value)
Adds the string representation of the specified object to the
end of this StringBuffer.
|
StringBuffer |
append(String string)
Adds the specified string to the end of this StringBuffer.
|
StringBuffer |
append(StringBuffer buffer)
Adds the specified StringBuffer to the end of this StringBuffer.
|
StringBuffer |
appendCodePoint(int codePoint)
Adds the specified code point to the end of this StringBuffer.
|
int |
capacity()
Answers the number of characters this StringBuffer can hold without
growing.
|
char |
charAt(int index)
Answers the character at the specified offset in this StringBuffer.
|
int |
codePointAt(int index)
Returns the Unicode character at the given point.
|
int |
codePointBefore(int index)
Returns the Unicode character before the given point.
|
int |
codePointCount(int start,
int end)
Returns the total Unicode values in the specified range.
|
StringBuffer |
delete(int start,
int end)
Deletes a range of characters starting from offset
start to offset
end - 1 . |
StringBuffer |
deleteCharAt(int location)
Deletes a single character
|
void |
ensureCapacity(int min)
Ensures that this StringBuffer can hold the specified number of characters
without growing.
|
void |
getChars(int start,
int end,
char[] buffer,
int index)
Copies the specified characters in this StringBuffer to the character array
starting at the specified offset in the character array.
|
int |
indexOf(String string)
Searches in this StringBuffer for the first index of the specified character.
|
int |
indexOf(String subString,
int start)
Searches in this StringBuffer for the index of the specified character.
|
StringBuffer |
insert(int index,
boolean value)
Inserts the string representation of the specified boolean at the specified
offset in this StringBuffer.
|
StringBuffer |
insert(int index,
char ch)
Inserts the character at the specified offset in this StringBuffer.
|
StringBuffer |
insert(int index,
char[] chars)
Inserts the character array at the specified offset in this StringBuffer.
|
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,
CharSequence sequence)
Inserts the CharSequence at the specified offset in this StringBuffer.
|
StringBuffer |
insert(int index,
CharSequence sequence,
int start,
int end)
Inserts the CharSequence at the specified offset in this StringBuffer.
|
StringBuffer |
insert(int index,
double value)
Inserts the string representation of the specified double at the specified
offset in this StringBuffer.
|
StringBuffer |
insert(int index,
float value)
Inserts the string representation of the specified float at the specified
offset in this StringBuffer.
|
StringBuffer |
insert(int index,
int value)
Inserts the string representation of the specified integer at the specified
offset in this StringBuffer.
|
StringBuffer |
insert(int index,
long value)
Inserts the string representation of the specified long at the specified
offset in this StringBuffer.
|
StringBuffer |
insert(int index,
Object value)
Inserts the string representation of the specified object at the specified
offset in this StringBuffer.
|
StringBuffer |
insert(int index,
String string)
Inserts the string at the specified offset in this StringBuffer.
|
int |
lastIndexOf(String string)
Searches in this StringBuffer for the last index of the specified character.
|
int |
lastIndexOf(String subString,
int start)
Searches in this StringBuffer for the index of the specified character.
|
int |
length()
Answers the size of this StringBuffer.
|
int |
offsetByCodePoints(int start,
int codePointCount)
Returns the index of the code point that was offset by
codePointCount . |
StringBuffer |
replace(int start,
int end,
String string)
Replace a range of characters with the characters in the specified String.
|
StringBuffer |
reverse()
Reverses the order of characters in this StringBuffer.
|
void |
setCharAt(int index,
char ch)
Sets the character at the specified offset in this StringBuffer.
|
void |
setLength(int length)
Sets the length of this StringBuffer to the specified length.
|
CharSequence |
subSequence(int start,
int end)
Copies a range of characters into a new String.
|
String |
substring(int start)
Copies a range of characters into a new String.
|
String |
substring(int start,
int end)
Copies a range of characters into a new String.
|
String |
toString()
Answers the contents of this StringBuffer.
|
void |
trimToSize()
Optionally modify the underlying char array to only
be large enough to hold the characters in this StringBuffer.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
chars, codePoints
public StringBuffer()
public StringBuffer(int capacity)
capacity
- the initial capacitypublic StringBuffer(String string)
string
- the initial contents of this StringBufferNullPointerException
- when string is nullpublic StringBuffer(CharSequence sequence)
sequence
- the initial contents of this StringBufferNullPointerException
- when sequence is nullpublic StringBuffer append(char[] chars)
chars
- the character arrayNullPointerException
- when chars is nullpublic StringBuffer append(char[] chars, int start, int length)
chars
- a character arraystart
- the starting offsetlength
- the number of charactersIndexOutOfBoundsException
- when length < 0, start < 0
or
start + length > chars.length
NullPointerException
- when chars is nullpublic StringBuffer append(char ch)
append
in interface Appendable
ch
- a characterpublic StringBuffer append(double value)
value
- the doublepublic StringBuffer append(float value)
value
- the floatpublic StringBuffer append(int value)
value
- the integerpublic StringBuffer append(long value)
value
- the longpublic StringBuffer append(Object value)
value
- the objectpublic StringBuffer append(String string)
string
- the stringpublic StringBuffer append(boolean value)
value
- the booleanpublic int capacity()
ensureCapacity(int)
,
length()
public char charAt(int index)
charAt
in interface CharSequence
index
- the zero-based index in this StringBufferIndexOutOfBoundsException
- If index < 0
or index >= length()
public StringBuffer delete(int start, int end)
start
to offset
end - 1
. If end
is beyond the last character, then this
method deletes up to the end of the StringBuffer.start
- the offset of the first characterend
- the offset one past the last characterStringIndexOutOfBoundsException
- when start < 0
, start > end
or
start > length()
public StringBuffer deleteCharAt(int location)
location
- the offset of the character to deleteStringIndexOutOfBoundsException
- when location < 0
or
location >= length()
public void ensureCapacity(int min)
min
- the minimum number of elements that this
StringBuffer will hold before growingpublic void getChars(int start, int end, char[] buffer, int index)
start
- the starting offset of characters to copyend
- the ending offset of characters to copybuffer
- the destination character arrayindex
- the starting offset in the character arrayIndexOutOfBoundsException
- when start < 0, end > length(),
start > end, index < 0, end - start > buffer.length - index
NullPointerException
- when buffer is nullpublic StringBuffer insert(int index, char[] chars)
index
- the index at which to insertchars
- the character array to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
NullPointerException
- when chars is nullpublic StringBuffer insert(int index, char[] chars, int start, int length)
index
- the index at which to insertchars
- a character arraystart
- the starting offsetlength
- the number of charactersStringIndexOutOfBoundsException
- when length < 0, start < 0,
start + length > chars.length, index < 0
or
index > length()
NullPointerException
- when chars is nullpublic StringBuffer insert(int index, char ch)
index
- the index at which to insertch
- the character to insertIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuffer insert(int index, double value)
index
- the index at which to insertvalue
- the double to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuffer insert(int index, float value)
index
- the index at which to insertvalue
- the float to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuffer insert(int index, int value)
index
- the index at which to insertvalue
- the integer to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuffer insert(int index, long value)
index
- the index at which to insertvalue
- the long to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuffer insert(int index, Object value)
index
- the index at which to insertvalue
- the object to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuffer insert(int index, String string)
index
- the index at which to insertstring
- the string to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuffer insert(int index, boolean value)
index
- the index at which to insertvalue
- the boolean to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public int length()
length
in interface CharSequence
public StringBuffer replace(int start, int end, String string)
start
- the offset of the first characterend
- the offset one past the last characterstring
- a StringStringIndexOutOfBoundsException
- when start < 0
or
start > end
public StringBuffer reverse()
public void setCharAt(int index, char ch)
index
- the zero-based index in this StringBufferch
- the characterIndexOutOfBoundsException
- when index < 0
or
index >= length()
public void setLength(int length)
\\u0000
.length
- the new length of this StringBufferIndexOutOfBoundsException
- when length < 0
length()
public String substring(int start)
start
- the offset of the first characterStringIndexOutOfBoundsException
- when start < 0
or
start > length()
public String substring(int start, int end)
start
- the offset of the first characterend
- the offset one past the last characterStringIndexOutOfBoundsException
- when start < 0, start > end
or
end > length()
public String toString()
toString
in interface CharSequence
toString
in class Object
public StringBuffer append(StringBuffer buffer)
buffer
- the StringBufferpublic CharSequence subSequence(int start, int end)
subSequence
in interface CharSequence
start
- the offset of the first characterend
- the offset one past the last characterIndexOutOfBoundsException
- when start < 0, start > end
or
end > length()
public int indexOf(String string)
string
- the string to findlastIndexOf(String)
public int indexOf(String subString, int start)
subString
- the string to findstart
- the starting offsetlastIndexOf(String,int)
public int lastIndexOf(String string)
string
- the string to findindexOf(String)
public int lastIndexOf(String subString, int start)
subString
- the string to findstart
- the starting offsetindexOf(String,int)
public StringBuffer append(CharSequence sequence)
append
in interface Appendable
sequence
- the CharSequencepublic StringBuffer append(CharSequence sequence, int start, int end)
append
in interface Appendable
sequence
- the CharSequencestart
- the offset of the first characterend
- the offset one past the last characterIndexOutOfBoundsException
- when start < 0, start > end
or
end > length()
public StringBuffer insert(int index, CharSequence sequence)
index
- the index at which to insertsequence
- the CharSequence to insertIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuffer insert(int index, CharSequence sequence, int start, int end)
index
- the index at which to insertsequence
- the CharSequence to insertstart
- the offset of the first characterend
- the offset one past the last characterIndexOutOfBoundsException
- when index < 0
or
index > length()
, or when start < 0, start > end
or
end > length()
public void trimToSize()
public int codePointAt(int index)
index
- the character indexpublic int codePointBefore(int index)
index
- the character indexpublic int codePointCount(int start, int end)
start
- first indexend
- last indexpublic int offsetByCodePoints(int start, int codePointCount)
codePointCount
.start
- the position to offsetcodePointCount
- the code point countpublic StringBuffer appendCodePoint(int codePoint)
codePoint
- the code pointEclipse OpenJ9 website.
To raise a bug report or suggest an improvement create an Eclipse OpenJ9 issue.
Copyright © 1993, 2023 IBM Corp. and others.