public final class StringBuilder extends Object implements Serializable, CharSequence, Appendable
Characters may be inserted at any position up to the length of the StringBuilder, increasing the length of the StringBuilder. Characters at any position in the StringBuilder may be replaced, which does not affect the StringBuilder length.
The capacity of a StringBuilder may be specified when the StringBuilder is created. If the capacity of the StringBuilder is exceeded, the capacity is increased.
StringBuffer
,
Serialized FormConstructor and Description |
---|
StringBuilder()
Constructs a new StringBuilder using the default capacity.
|
StringBuilder(CharSequence sequence)
Constructs a new StringBuilder containing the characters in
the specified CharSequence and the default capacity.
|
StringBuilder(int capacity)
Constructs a new StringBuilder using the specified capacity.
|
StringBuilder(String string)
Constructs a new StringBuilder containing the characters in
the specified string and the default capacity.
|
Modifier and Type | Method and Description |
---|---|
StringBuilder |
append(boolean value)
Adds the string representation of the specified boolean to the
end of this StringBuilder.
|
StringBuilder |
append(char ch)
Adds the specified character to the end of
this StringBuilder.
|
StringBuilder |
append(char[] chars)
Adds the character array to the end of this StringBuilder.
|
StringBuilder |
append(char[] chars,
int start,
int length)
Adds the specified sequence of characters to the end of
this StringBuilder.
|
StringBuilder |
append(CharSequence sequence)
Adds the specified CharSequence to the end of this StringBuilder.
|
StringBuilder |
append(CharSequence sequence,
int start,
int end)
Adds the specified CharSequence to the end of this StringBuilder.
|
StringBuilder |
append(double value)
Adds the string representation of the specified double to the
end of this StringBuilder.
|
StringBuilder |
append(float value)
Adds the string representation of the specified float to the
end of this StringBuilder.
|
StringBuilder |
append(int value)
Adds the string representation of the specified integer to the
end of this StringBuilder.
|
StringBuilder |
append(long value)
Adds the string representation of the specified long to the
end of this StringBuilder.
|
StringBuilder |
append(Object value)
Adds the string representation of the specified object to the
end of this StringBuilder.
|
StringBuilder |
append(String string)
Adds the specified string to the end of this StringBuilder.
|
StringBuilder |
append(StringBuffer buffer)
Adds the specified StringBuffer to the end of this StringBuilder.
|
StringBuilder |
appendCodePoint(int codePoint)
Adds the specified code point to the end of this StringBuilder.
|
int |
capacity()
Answers the number of characters this StringBuilder can hold without
growing.
|
char |
charAt(int index)
Answers the character at the specified offset in this StringBuilder.
|
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.
|
StringBuilder |
delete(int start,
int end)
Deletes a range of characters starting from offset
start to offset
end - 1 . |
StringBuilder |
deleteCharAt(int location)
Deletes a single character
|
void |
ensureCapacity(int min)
Ensures that this StringBuilder 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 StringBuilder to the character array
starting at the specified offset in the character array.
|
int |
indexOf(String string)
Searches in this StringBuilder for the first index of the specified character.
|
int |
indexOf(String subString,
int start)
Searches in this StringBuilder for the index of the specified character.
|
StringBuilder |
insert(int index,
boolean value)
Inserts the string representation of the specified boolean at the specified
offset in this StringBuilder.
|
StringBuilder |
insert(int index,
char ch)
Inserts the character at the specified offset in this StringBuilder.
|
StringBuilder |
insert(int index,
char[] chars)
Inserts the character array at the specified offset in this StringBuilder.
|
StringBuilder |
insert(int index,
char[] chars,
int start,
int length)
Inserts the specified sequence of characters at the
specified offset in this StringBuilder.
|
StringBuilder |
insert(int index,
CharSequence sequence)
Inserts the CharSequence at the specified offset in this StringBuilder.
|
StringBuilder |
insert(int index,
CharSequence sequence,
int start,
int end)
Inserts the CharSequence at the specified offset in this StringBuilder.
|
StringBuilder |
insert(int index,
double value)
Inserts the string representation of the specified double at the specified
offset in this StringBuilder.
|
StringBuilder |
insert(int index,
float value)
Inserts the string representation of the specified float at the specified
offset in this StringBuilder.
|
StringBuilder |
insert(int index,
int value)
Inserts the string representation of the specified integer at the specified
offset in this StringBuilder.
|
StringBuilder |
insert(int index,
long value)
Inserts the string representation of the specified long at the specified
offset in this StringBuilder.
|
StringBuilder |
insert(int index,
Object value)
Inserts the string representation of the specified object at the specified
offset in this StringBuilder.
|
StringBuilder |
insert(int index,
String string)
Inserts the string at the specified offset in this StringBuilder.
|
int |
lastIndexOf(String string)
Searches in this StringBuilder for the last index of the specified character.
|
int |
lastIndexOf(String subString,
int start)
Searches in this StringBuilder for the index of the specified character.
|
int |
length()
Answers the size of this StringBuilder.
|
int |
offsetByCodePoints(int start,
int codePointCount)
Returns the index of the code point that was offset by
codePointCount . |
StringBuilder |
replace(int start,
int end,
String string)
Replace a range of characters with the characters in the specified String.
|
StringBuilder |
reverse()
Reverses the order of characters in this StringBuilder.
|
void |
setCharAt(int index,
char ch)
Sets the character at the specified offset in this StringBuilder.
|
void |
setLength(int length)
Sets the length of this StringBuilder 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 StringBuilder.
|
void |
trimToSize()
Optionally modify the underlying char array to only
be large enough to hold the characters in this StringBuilder.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
chars, codePoints
public StringBuilder()
public StringBuilder(int capacity)
capacity
- the initial capacitypublic StringBuilder(String string)
string
- the initial contents of this StringBuilderNullPointerException
- when string is nullpublic StringBuilder(CharSequence sequence)
sequence
- the initial contents of this StringBuilderNullPointerException
- when sequence is nullpublic StringBuilder append(char[] chars)
chars
- the character arrayNullPointerException
- when chars is nullpublic StringBuilder 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 StringBuilder append(char ch)
append
in interface Appendable
ch
- a characterpublic StringBuilder append(double value)
value
- the doublepublic StringBuilder append(float value)
value
- the floatpublic StringBuilder append(int value)
value
- the integerpublic StringBuilder append(long value)
value
- the longpublic StringBuilder append(Object value)
value
- the objectpublic StringBuilder append(String string)
string
- the stringpublic StringBuilder 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 StringBuilderIndexOutOfBoundsException
- when index < 0
or
index >= length()
public StringBuilder 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 StringBuilder.start
- the offset of the first characterend
- the offset one past the last characterStringIndexOutOfBoundsException
- when start < 0
, start > end
or
start > length()
public StringBuilder 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
StringBuilder 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 StringBuilder 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 StringBuilder 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 StringBuilder insert(int index, char ch)
index
- the index at which to insertch
- the character to insertIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuilder insert(int index, double value)
index
- the index at which to insertvalue
- the double to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuilder insert(int index, float value)
index
- the index at which to insertvalue
- the float to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuilder insert(int index, int value)
index
- the index at which to insertvalue
- the integer to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuilder insert(int index, long value)
index
- the index at which to insertvalue
- the long to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuilder insert(int index, Object value)
index
- the index at which to insertvalue
- the object to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuilder insert(int index, String string)
index
- the index at which to insertstring
- the string to insertStringIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuilder 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 StringBuilder 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 StringBuilder reverse()
public void setCharAt(int index, char ch)
index
- the zero-based index in this StringBuilderch
- the characterIndexOutOfBoundsException
- when index < 0
or
index >= length()
public void setLength(int length)
\\u0000
.length
- the new length of this StringBuilderIndexOutOfBoundsException
- 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 StringBuilder 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 StringBuilder append(CharSequence sequence)
append
in interface Appendable
sequence
- the CharSequencepublic StringBuilder 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 StringBuilder insert(int index, CharSequence sequence)
index
- the index at which to insertsequence
- the CharSequence to insertIndexOutOfBoundsException
- when index < 0
or
index > length()
public StringBuilder 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 StringBuilder 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.