public final class StringBuilder extends Object implements Serializable, CharSequence, Appendable
StringBuilder
represents a changeable String
.
It provides the operations required to modify the
StringBuilder
, including insert, replace, delete, append,
and reverse. It like StringBuffer
, but is not
synchronized. It is ideal for use when it is known that the
object will only be used from a single thread.
StringBuilder
s are variable-length in nature, so even if
you initialize them to a certain size, they can still grow larger than
that. Capacity indicates the number of characters the
StringBuilder
can have in it before it has to grow (growing
the char array is an expensive operation involving new
).
Incidentally, compilers often implement the String operator "+"
by using a StringBuilder
operation:
a + b
is the same as
new StringBuilder().append(a).append(b).toString()
.
Classpath's StringBuilder is capable of sharing memory with Strings for efficiency. This will help when a StringBuilder is converted to a String and the StringBuilder is not changed after that (quite common when performing string concatenation).
String
,
StringBuffer
,
Serialized FormConstructor and Description |
---|
StringBuilder()
Create a new StringBuilder with default capacity 16.
|
StringBuilder(CharSequence seq)
Create a new
StringBuilder with the characters in the
specified CharSequence . |
StringBuilder(int capacity)
Create an empty
StringBuilder with the specified initial
capacity. |
StringBuilder(String str)
Create a new
StringBuilder with the characters in the
specified String . |
Modifier and Type | Method and Description |
---|---|
StringBuilder |
append(boolean bool)
Append the
String value of the argument to this
StringBuilder . |
StringBuilder |
append(char ch)
Append the
char to this StringBuilder . |
StringBuilder |
append(char[] data)
Append the
char array to this StringBuilder . |
StringBuilder |
append(char[] data,
int offset,
int count)
Append part of the
char array to this
StringBuilder . |
StringBuilder |
append(CharSequence seq)
Append the characters in the
CharSequence to this
buffer. |
StringBuilder |
append(CharSequence seq,
int start,
int end)
Append some characters from the
CharSequence to this
buffer. |
StringBuilder |
append(double dnum)
Append the
String value of the argument to this
StringBuilder . |
StringBuilder |
append(float fnum)
Append the
String value of the argument to this
StringBuilder . |
StringBuilder |
append(int inum)
Append the
String value of the argument to this
StringBuilder . |
StringBuilder |
append(long lnum)
Append the
String value of the argument to this
StringBuilder . |
StringBuilder |
append(Object obj)
Append the
String value of the argument to this
StringBuilder . |
StringBuilder |
append(String str)
Append the
String to this StringBuilder . |
StringBuilder |
append(StringBuffer stringBuffer)
Append the
StringBuilder value of the argument to this
StringBuilder . |
StringBuilder |
appendCodePoint(int code)
Append the code point to this
StringBuilder . |
int |
capacity()
Get the total number of characters this
StringBuilder can
support before it must be grown. |
char |
charAt(int index)
Get the character at the specified index.
|
int |
codePointAt(int index)
Get the code point at the specified index.
|
int |
codePointBefore(int index)
Get the code point before the specified index.
|
int |
codePointCount(int start,
int end)
Return the number of code points between two indices in the
StringBuffer . |
StringBuilder |
delete(int start,
int end)
Delete characters from this
StringBuilder . |
StringBuilder |
deleteCharAt(int index)
Delete a character from this
StringBuilder . |
void |
ensureCapacity(int minimumCapacity)
Increase the capacity of this
StringBuffer . |
void |
getChars(int srcOffset,
int srcEnd,
char[] dst,
int dstOffset)
Get the specified array of characters.
|
int |
indexOf(String str)
Finds the first instance of a substring in this StringBuilder.
|
int |
indexOf(String str,
int fromIndex)
Finds the first instance of a String in this StringBuffer, starting at
a given index.
|
StringBuilder |
insert(int offset,
boolean bool)
Insert the
String value of the argument into this
StringBuilder . |
StringBuilder |
insert(int offset,
char ch)
Insert the
char argument into this StringBuilder . |
StringBuilder |
insert(int offset,
char[] data)
Insert the
char[] argument into this
StringBuilder . |
StringBuilder |
insert(int offset,
char[] str,
int str_offset,
int len)
Insert a subarray of the
char[] argument into this
StringBuilder . |
StringBuilder |
insert(int offset,
CharSequence sequence)
Insert the
CharSequence argument into this
StringBuilder . |
StringBuilder |
insert(int offset,
CharSequence sequence,
int start,
int end)
Insert a subsequence of the
CharSequence argument into this
StringBuilder . |
StringBuilder |
insert(int offset,
double dnum)
Insert the
String value of the argument into this
StringBuilder . |
StringBuilder |
insert(int offset,
float fnum)
Insert the
String value of the argument into this
StringBuilder . |
StringBuilder |
insert(int offset,
int inum)
Insert the
String value of the argument into this
StringBuilder . |
StringBuilder |
insert(int offset,
long lnum)
Insert the
String value of the argument into this
StringBuilder . |
StringBuilder |
insert(int offset,
Object obj)
Insert the
String value of the argument into this
StringBuilder . |
StringBuilder |
insert(int offset,
String str)
Insert the
String argument into this
StringBuilder . |
int |
lastIndexOf(String str)
Finds the last instance of a substring in this StringBuffer.
|
int |
lastIndexOf(String str,
int fromIndex)
Finds the last instance of a String in this StringBuffer, starting at a
given index.
|
int |
length()
Get the length of the
String this StringBuilder
would create. |
int |
offsetByCodePoints(int start,
int codePoints)
Starting at the given index, this counts forward by the indicated
number of code points, and then returns the resulting index.
|
StringBuilder |
replace(int start,
int end,
String str)
Replace characters between index
start (inclusive) and
end (exclusive) with str . |
StringBuilder |
reverse()
Reverse the characters in this StringBuilder.
|
void |
setCharAt(int index,
char ch)
Set the character at the specified index.
|
void |
setLength(int newLength)
Set the length of this StringBuffer.
|
CharSequence |
subSequence(int beginIndex,
int endIndex)
Creates a substring of this StringBuilder, starting at a specified index
and ending at one character before a specified index.
|
String |
substring(int beginIndex)
Creates a substring of this StringBuilder, starting at a specified index
and ending at the end of this StringBuilder.
|
String |
substring(int beginIndex,
int endIndex)
Creates a substring of this StringBuilder, starting at a specified index
and ending at one character before a specified index.
|
String |
toString()
Convert this
StringBuilder to a String . |
void |
trimToSize()
This may reduce the amount of memory used by the StringBuffer,
by resizing the internal array to remove unused space.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
charAt
public StringBuilder()
public StringBuilder(int capacity)
StringBuilder
with the specified initial
capacity.capacity
- the initial capacityNegativeArraySizeException
- if capacity is negativepublic StringBuilder(String str)
StringBuilder
with the characters in the
specified String
. Initial capacity will be the size of the
String plus 16.str
- the String
to convertNullPointerException
- if str is nullpublic StringBuilder(CharSequence seq)
StringBuilder
with the characters in the
specified CharSequence
. Initial capacity will be the
length of the sequence plus 16; if the sequence reports a length
less than or equal to 0, then the initial capacity will be 16.seq
- the initializing CharSequence
NullPointerException
- if str is nullpublic int length()
String
this StringBuilder
would create. Not to be confused with the capacity of the
StringBuilder
.length
in interface CharSequence
StringBuilder
capacity()
,
setLength(int)
public int capacity()
StringBuilder
can
support before it must be grown. Not to be confused with length.StringBuilder
length()
,
ensureCapacity(int)
public StringBuilder append(Object obj)
String
value of the argument to this
StringBuilder
. Uses String.valueOf()
to convert
to String
.obj
- the Object
to convert and appendStringBuilder
String.valueOf(Object)
,
append(String)
public StringBuilder append(String str)
String
to this StringBuilder
. If
str is null, the String "null" is appended.str
- the String
to appendStringBuilder
public StringBuilder append(StringBuffer stringBuffer)
StringBuilder
value of the argument to this
StringBuilder
. This behaves the same as
append((Object) stringBuffer)
, except it is more efficient.stringBuffer
- the StringBuilder
to convert and appendStringBuilder
append(Object)
public StringBuilder append(char[] data)
char
array to this StringBuilder
.
This is similar (but more efficient) than
append(new String(data))
, except in the case of null.data
- the char[]
to appendStringBuilder
NullPointerException
- if str
is null
append(char[], int, int)
public StringBuilder append(char[] data, int offset, int count)
char
array to this
StringBuilder
. This is similar (but more efficient) than
append(new String(data, offset, count))
, except in the case
of null.data
- the char[]
to appendoffset
- the start location in str
count
- the number of characters to get from str
StringBuilder
NullPointerException
- if str
is null
IndexOutOfBoundsException
- if offset or count is out of range
(while unspecified, this is a StringIndexOutOfBoundsException)public StringBuilder append(boolean bool)
String
value of the argument to this
StringBuilder
. Uses String.valueOf()
to convert
to String
.bool
- the boolean
to convert and appendStringBuilder
String.valueOf(boolean)
public StringBuilder append(char ch)
char
to this StringBuilder
.append
in interface Appendable
ch
- the char
to appendStringBuilder
public StringBuilder append(CharSequence seq)
CharSequence
to this
buffer.append
in interface Appendable
seq
- the CharSequence
providing the charactersStringBuilder
public StringBuilder append(CharSequence seq, int start, int end)
CharSequence
to this
buffer. If the argument is null, the four characters "null" are
appended.append
in interface Appendable
seq
- the CharSequence
providing the charactersstart
- the starting indexend
- one past the final indexStringBuilder
public StringBuilder append(int inum)
String
value of the argument to this
StringBuilder
. Uses String.valueOf()
to convert
to String
.inum
- the int
to convert and appendStringBuilder
String.valueOf(int)
public StringBuilder append(long lnum)
String
value of the argument to this
StringBuilder
. Uses String.valueOf()
to convert
to String
.lnum
- the long
to convert and appendStringBuilder
String.valueOf(long)
public StringBuilder append(float fnum)
String
value of the argument to this
StringBuilder
. Uses String.valueOf()
to convert
to String
.fnum
- the float
to convert and appendStringBuilder
String.valueOf(float)
public StringBuilder append(double dnum)
String
value of the argument to this
StringBuilder
. Uses String.valueOf()
to convert
to String
.dnum
- the double
to convert and appendStringBuilder
String.valueOf(double)
public StringBuilder appendCodePoint(int code)
StringBuilder
.
This is like #append(char), but will append two characters
if a supplementary code point is given.code
- the code point to appendStringBuilder
Character.toChars(int, char[], int)
public StringBuilder delete(int start, int end)
StringBuilder
.
delete(10, 12)
will delete 10 and 11, but not 12. It is
harmless for end to be larger than length().start
- the first character to deleteend
- the index after the last character to deleteStringBuilder
StringIndexOutOfBoundsException
- if start or end are out of boundspublic StringBuilder deleteCharAt(int index)
StringBuilder
.index
- the index of the character to deleteStringBuilder
StringIndexOutOfBoundsException
- if index is out of boundspublic StringBuilder replace(int start, int end, String str)
start
(inclusive) and
end
(exclusive) with str
. If end
is larger than the size of this StringBuilder, all characters after
start
are replaced.start
- the beginning index of characters to delete (inclusive)end
- the ending index of characters to delete (exclusive)str
- the new String
to insertStringBuilder
StringIndexOutOfBoundsException
- if start or end are out of boundsNullPointerException
- if str is nullpublic String substring(int beginIndex)
beginIndex
- index to start substring (base 0)StringIndexOutOfBoundsException
- if beginIndex is out of boundssubstring(int, int)
public CharSequence subSequence(int beginIndex, int endIndex)
substring(beginIndex, endIndex)
, to satisfy
the CharSequence interface.subSequence
in interface CharSequence
beginIndex
- index to start at (inclusive, base 0)endIndex
- index to end at (exclusive)IndexOutOfBoundsException
- if beginIndex or endIndex is out of
boundssubstring(int, int)
public String substring(int beginIndex, int endIndex)
beginIndex
- index to start at (inclusive, base 0)endIndex
- index to end at (exclusive)StringIndexOutOfBoundsException
- if beginIndex or endIndex is out
of boundspublic StringBuilder insert(int offset, char[] str, int str_offset, int len)
char[]
argument into this
StringBuilder
.offset
- the place to insert in this bufferstr
- the char[]
to insertstr_offset
- the index in str
to start inserting fromlen
- the number of characters to insertStringBuilder
NullPointerException
- if str
is null
StringIndexOutOfBoundsException
- if any index is out of boundspublic StringBuilder insert(int offset, Object obj)
String
value of the argument into this
StringBuilder
. Uses String.valueOf()
to convert
to String
.offset
- the place to insert in this bufferobj
- the Object
to convert and insertStringBuilder
StringIndexOutOfBoundsException
- if offset is out of boundsString.valueOf(Object)
public StringBuilder insert(int offset, String str)
String
argument into this
StringBuilder
. If str is null, the String "null" is used
instead.offset
- the place to insert in this bufferstr
- the String
to insertStringBuilder
StringIndexOutOfBoundsException
- if offset is out of boundspublic StringBuilder insert(int offset, CharSequence sequence)
CharSequence
argument into this
StringBuilder
. If the sequence is null, the String
"null" is used instead.offset
- the place to insert in this buffersequence
- the CharSequence
to insertStringBuilder
IndexOutOfBoundsException
- if offset is out of boundspublic StringBuilder insert(int offset, CharSequence sequence, int start, int end)
CharSequence
argument into this
StringBuilder
. If the sequence is null, the String
"null" is used instead.offset
- the place to insert in this buffersequence
- the CharSequence
to insertstart
- the starting index of the subsequenceend
- one past the ending index of the subsequenceStringBuilder
IndexOutOfBoundsException
- if offset, start,
or end are out of boundspublic StringBuilder insert(int offset, char[] data)
char[]
argument into this
StringBuilder
.offset
- the place to insert in this bufferdata
- the char[]
to insertStringBuilder
NullPointerException
- if data
is null
StringIndexOutOfBoundsException
- if offset is out of boundsinsert(int, char[], int, int)
public StringBuilder insert(int offset, boolean bool)
String
value of the argument into this
StringBuilder
. Uses String.valueOf()
to convert
to String
.offset
- the place to insert in this bufferbool
- the boolean
to convert and insertStringBuilder
StringIndexOutOfBoundsException
- if offset is out of boundsString.valueOf(boolean)
public StringBuilder insert(int offset, char ch)
char
argument into this StringBuilder
.offset
- the place to insert in this bufferch
- the char
to insertStringBuilder
StringIndexOutOfBoundsException
- if offset is out of boundspublic StringBuilder insert(int offset, int inum)
String
value of the argument into this
StringBuilder
. Uses String.valueOf()
to convert
to String
.offset
- the place to insert in this bufferinum
- the int
to convert and insertStringBuilder
StringIndexOutOfBoundsException
- if offset is out of boundsString.valueOf(int)
public StringBuilder insert(int offset, long lnum)
String
value of the argument into this
StringBuilder
. Uses String.valueOf()
to convert
to String
.offset
- the place to insert in this bufferlnum
- the long
to convert and insertStringBuilder
StringIndexOutOfBoundsException
- if offset is out of boundsString.valueOf(long)
public StringBuilder insert(int offset, float fnum)
String
value of the argument into this
StringBuilder
. Uses String.valueOf()
to convert
to String
.offset
- the place to insert in this bufferfnum
- the float
to convert and insertStringBuilder
StringIndexOutOfBoundsException
- if offset is out of boundsString.valueOf(float)
public StringBuilder insert(int offset, double dnum)
String
value of the argument into this
StringBuilder
. Uses String.valueOf()
to convert
to String
.offset
- the place to insert in this bufferdnum
- the double
to convert and insertStringBuilder
StringIndexOutOfBoundsException
- if offset is out of boundsString.valueOf(double)
public StringBuilder reverse()
StringBuilder
public String toString()
StringBuilder
to a String
. The
String is composed of the characters currently in this StringBuilder. Note
that the result is a copy, and that future modifications to this buffer
do not affect the String.toString
in interface CharSequence
toString
in class Object
Object.getClass()
,
Object.hashCode()
,
Class.getName()
,
Integer.toHexString(int)
public void ensureCapacity(int minimumCapacity)
StringBuffer
. This will
ensure that an expensive growing operation will not occur until
minimumCapacity
is reached. The buffer is grown to the
larger of minimumCapacity
and
capacity() * 2 + 2
, if it is not already large enough.minimumCapacity
- the new capacity#capacity()
public void setLength(int newLength)
newLength
characters of the old array will be preserved, and the remaining
characters are truncated.newLength
- the new lengthIndexOutOfBoundsException
- if the new length is negative
(while unspecified, this is a StringIndexOutOfBoundsException)CharSequence.length()
public char charAt(int index)
charAt
in interface CharSequence
index
- the index of the character to get, starting at 0IndexOutOfBoundsException
- if index is negative or >= length()
(while unspecified, this is a StringIndexOutOfBoundsException)public int codePointAt(int index)
index
- the index of the codepoint to get, starting at 0IndexOutOfBoundsException
- if index is negative or >= length()public int codePointBefore(int index)
index-1
and
index-2
to see if they form a supplementary code point.index
- the index just past the codepoint to get, starting at 0IndexOutOfBoundsException
- if index is negative or >= length()public void getChars(int srcOffset, int srcEnd, char[] dst, int dstOffset)
srcOffset - srcEnd
characters will be copied into the array you pass in.srcOffset
- the index to start copying from (inclusive)srcEnd
- the index to stop copying from (exclusive)dst
- the array to copy intodstOffset
- the index to start copying intoNullPointerException
- if dst is nullIndexOutOfBoundsException
- if any source or target indices are
out of range (while unspecified, source problems cause a
StringIndexOutOfBoundsException, and dest problems cause an
ArrayIndexOutOfBoundsException)System.arraycopy(Object, int, Object, int, int)
public void setCharAt(int index, char ch)
index
- the index of the character to set starting at 0ch
- the value to set that character toIndexOutOfBoundsException
- if index is negative or >= length()
(while unspecified, this is a StringIndexOutOfBoundsException)public int indexOf(String str)
str
- String to findNullPointerException
- if str is nullindexOf(String, int)
public int indexOf(String str, int fromIndex)
str
- String to findfromIndex
- index to start the searchNullPointerException
- if str is nullpublic int lastIndexOf(String str)
str
- String to findNullPointerException
- if str is nulllastIndexOf(String, int)
public int lastIndexOf(String str, int fromIndex)
str
- String to findfromIndex
- index to start the searchNullPointerException
- if str is nullpublic void trimToSize()
public int codePointCount(int start, int end)
StringBuffer
. An unpaired surrogate counts as a
code point for this purpose. Characters outside the indicated
range are not examined, even if the range ends in the middle of a
surrogate pair.start
- the starting indexend
- one past the ending indexpublic int offsetByCodePoints(int start, int codePoints)
start
- the starting indexcodePoints
- the number of code points