java.lang.Object | |
↳ | java.lang.StringBuilder |
A modifiable
sequence of characters
for use in creating
strings. This class is intended as a direct replacement of
StringBuffer
for non-concurrent use; unlike
StringBuffer
this
class is not synchronized.
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 StringBuilder("a").append("b").append("c").toString()
.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Constructs an instance with an initial capacity of
16
.
|
||||||||||
|
Constructs an instance with the specified capacity.
|
||||||||||
|
Constructs an instance that's initialized with the contents of the
specified
CharSequence
.
|
||||||||||
|
Constructs an instance that's initialized with the contents of the
specified
String
.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Appends the string representation of the specified subset of the
char[]
.
|
||||||||||
|
Appends the string representation of the specified
double
value.
|
||||||||||
|
Appends the string representation of the specified
long
value.
|
||||||||||
|
Appends the string representation of the specified
boolean
value.
|
||||||||||
|
Appends the string representation of the specified
float
value.
|
||||||||||
|
Appends the string representation of the specified
char[]
.
|
||||||||||
|
Appends the string representation of the specified
int
value.
|
||||||||||
|
Appends the contents of the specified
StringBuffer
.
|
||||||||||
|
Appends the string representation of the specified
CharSequence
.
|
||||||||||
|
Appends the string representation of the specified
char
value.
|
||||||||||
|
Appends the contents of the specified string.
|
||||||||||
|
Appends the string representation of the specified subsequence of the
CharSequence
.
|
||||||||||
|
Appends the string representation of the specified
Object
.
|
||||||||||
|
Appends the encoded Unicode code point.
|
||||||||||
|
Returns the number of characters that can be held without growing.
|
||||||||||
|
Returns the character at
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 sequence of characters specified by
start
and
end
.
|
||||||||||
|
Deletes the character at the specified index.
|
||||||||||
|
Ensures that this object has a minimum capacity available before
requiring the internal buffer to be enlarged.
|
||||||||||
|
Copies the requested sequence of characters into
dst
passed
starting at
dst
.
|
||||||||||
|
Searches for the index of the specified character.
|
||||||||||
|
Searches for the first index of the specified character.
|
||||||||||
|
Inserts the string representation of the specified subsequence of the
CharSequence
at the specified
offset
.
|
||||||||||
|
Inserts the string representation of the specified
int
value at
the specified
offset
.
|
||||||||||
|
Inserts the string representation of the specified
double
value
at the specified
offset
.
|
||||||||||
|
Inserts the string representation of the specified subsequence of the
char[]
at the specified
offset
.
|
||||||||||
|
Inserts the specified string at the specified
offset
.
|
||||||||||
|
Inserts the string representation of the specified
long
value at
the specified
offset
.
|
||||||||||
|
Inserts the string representation of the specified
Object
at the
specified
offset
.
|
||||||||||
|
Inserts the string representation of the specified
float
value at
the specified
offset
.
|
||||||||||
|
Inserts the string representation of the specified
char
value at
the specified
offset
.
|
||||||||||
|
Inserts the string representation of the specified
char[]
at the
specified
offset
.
|
||||||||||
|
Inserts the string representation of the specified
CharSequence
at the specified
offset
.
|
||||||||||
|
Inserts the string representation of the specified
boolean
value
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 specified subsequence in this builder with the specified
string.
|
||||||||||
|
Reverses the order of characters in this builder.
|
||||||||||
|
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 contents of this builder.
|
||||||||||
|
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 an instance with an initial capacity of
16
.
Constructs an instance with the specified capacity.
capacity | the initial capacity to use. |
---|
NegativeArraySizeException |
if the specified
capacity
is negative.
|
---|
Constructs an instance that's initialized with the contents of the
specified
CharSequence
. The capacity of the new builder will be
the length of the
CharSequence
plus 16.
seq |
the
CharSequence
to copy into the builder.
|
---|
NullPointerException |
if
seq
is
null
.
|
---|
Constructs an instance that's initialized with the contents of the
specified
String
. The capacity of the new builder will be the
length of the
String
plus 16.
str |
the
String
to copy into the builder.
|
---|
NullPointerException |
if
str
is
null
.
|
---|
Appends the string representation of the specified subset of the
char[]
. The
char[]
value is converted to a String according to
the rule defined by
valueOf(char[], int, int)
.
str |
the
char[]
to append.
|
---|---|
offset | the inclusive offset index. |
len | the number of characters. |
ArrayIndexOutOfBoundsException |
if
offset
and
len
do not specify a valid
subsequence.
|
---|
Appends the string representation of the specified
double
value.
The
double
value is converted to a string according to the rule
defined by
valueOf(double)
.
d |
the
double
value to append.
|
---|
Appends the string representation of the specified
long
value.
The
long
value is converted to a string according to the rule
defined by
valueOf(long)
.
l |
the
long
value.
|
---|
Appends the string representation of the specified
boolean
value.
The
boolean
value is converted to a String according to the rule
defined by
valueOf(boolean)
.
b |
the
boolean
value to append.
|
---|
Appends the string representation of the specified
float
value.
The
float
value is converted to a string according to the rule
defined by
valueOf(float)
.
f |
the
float
value to append.
|
---|
Appends the string representation of the specified
char[]
.
The
char[]
is converted to a string according to the rule
defined by
valueOf(char[])
.
chars |
the
char[]
to append..
|
---|
Appends the string representation of the specified
int
value. The
int
value is converted to a string according to the rule defined
by
valueOf(int)
.
i |
the
int
value to append.
|
---|
Appends the contents of the specified
StringBuffer
. If the
StringBuffer is
null
, then the string
"null"
is
appended.
sb |
the
StringBuffer
to append.
|
---|
Appends the string representation of the specified
CharSequence
.
If the
CharSequence
is
null
, then the string
"null"
is appended.
csq |
the
CharSequence
to append.
|
---|
Appends the string representation of the specified
char
value.
The
char
value is converted to a string according to the rule
defined by
valueOf(char)
.
c |
the
char
value to append.
|
---|
Appends the contents of the specified string. If the string is
null
, then the string
"null"
is appended.
str | the string to append. |
---|
Appends the string representation of the specified subsequence of the
CharSequence
. If the
CharSequence
is
null
, then
the string
"null"
is used to extract the subsequence from.
csq |
the
CharSequence
to append.
|
---|---|
start | the beginning index. |
end | the ending index. |
IndexOutOfBoundsException |
if
start
or
end
are negative,
start
is greater than
end
or
end
is greater than
the length of
csq
.
|
---|
Appends the string representation of the specified
Object
.
The
Object
value is converted to a string according to the rule
defined by
valueOf(Object)
.
obj |
the
Object
to append.
|
---|
Appends the encoded Unicode code point. 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.
Returns the character at
index
.
IndexOutOfBoundsException |
if
index < 0
or
index >= length()
.
|
---|
Retrieves the Unicode code point value at the
index
.
index |
the index to the
char
code unit.
|
---|
IndexOutOfBoundsException |
if
index
is negative or greater than or equal to
length()
.
|
---|
Retrieves the Unicode code point value that precedes the
index
.
index |
the index to the
char
code unit within this object.
|
---|
IndexOutOfBoundsException |
if
index
is less than 1 or greater than
length()
.
|
---|
Calculates the number of Unicode code points between
start
and
end
.
start | the inclusive beginning index of the subsequence. |
---|---|
end | the exclusive end index of the subsequence. |
IndexOutOfBoundsException |
if
start
is negative or greater than
end
or
end
is greater than
length()
.
|
---|
Deletes a sequence of characters specified by
start
and
end
. Shifts any remaining characters to the left.
start | the inclusive start index. |
---|---|
end | the exclusive end index. |
StringIndexOutOfBoundsException |
if
start
is less than zero, greater than the current
length or greater than
end
.
|
---|
Deletes the character at the specified index. shifts any remaining characters to the left.
index | the index of the character to delete. |
---|
StringIndexOutOfBoundsException |
if
index
is less than zero or is greater than or
equal to the current 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 into
dst
passed
starting at
dst
.
start | the inclusive start index of the characters to copy. |
---|---|
end | the exclusive end index of the characters to copy. |
dst |
the
char[]
to copy the characters to.
|
dstStart |
the inclusive start index of
dst
to begin copying to.
|
IndexOutOfBoundsException |
if the
start
is negative, the
dstStart
is
negative, the
start
is greater than
end
, the
end
is greater than the current
length()
or
dstStart + end - begin
is greater than
dst.length
.
|
---|
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 string representation of the specified subsequence of the
CharSequence
at the specified
offset
. The
CharSequence
is converted to a String as defined by
subSequence(int, int)
. If the
CharSequence
is
null
, then the string
"null"
is used to determine the
subsequence.
offset | the index to insert at. |
---|---|
s |
the
CharSequence
to insert.
|
start | the start of the subsequence of the character sequence. |
end | the end of the subsequence of the character sequence. |
IndexOutOfBoundsException |
if
offset
is negative or greater than the current
length()
, or
start
and
end
do not
specify a valid subsequence.
|
---|
Inserts the string representation of the specified
int
value at
the specified
offset
. The
int
value is converted to a
String according to the rule defined by
valueOf(int)
.
offset | the index to insert at. |
---|---|
i |
the
int
value to insert.
|
StringIndexOutOfBoundsException |
if
offset
is negative or greater than the current
length()
.
|
---|
Inserts the string representation of the specified
double
value
at the specified
offset
. The
double
value is converted
to a String according to the rule defined by
valueOf(double)
.
offset | the index to insert at. |
---|---|
d |
the
double
value to insert.
|
StringIndexOutOfBoundsException |
if
offset
is negative or greater than the current
length()
.
|
---|
Inserts the string representation of the specified subsequence of the
char[]
at the specified
offset
. The
char[]
value
is converted to a String according to the rule defined by
valueOf(char[], int, int)
.
offset | the index to insert at. |
---|---|
str |
the
char[]
to insert.
|
strOffset | the inclusive index. |
strLen | the number of characters. |
StringIndexOutOfBoundsException |
if
offset
is negative or greater than the current
length()
, or
strOffset
and
strLen
do
not specify a valid subsequence.
|
---|
Inserts the specified string at the specified
offset
. If the
specified string is null, then the String
"null"
is inserted.
offset | the index to insert at. |
---|---|
str |
the
String
to insert.
|
StringIndexOutOfBoundsException |
if
offset
is negative or greater than the current
length()
.
|
---|
Inserts the string representation of the specified
long
value at
the specified
offset
. The
long
value is converted to a
String according to the rule defined by
valueOf(long)
.
offset | the index to insert at. |
---|---|
l |
the
long
value to insert.
|
StringIndexOutOfBoundsException |
if
offset
is negative or greater than the current
{code length()}.
|
---|
Inserts the string representation of the specified
Object
at the
specified
offset
. The
Object
value is converted to a
String according to the rule defined by
valueOf(Object)
.
offset | the index to insert at. |
---|---|
obj |
the
Object
to insert.
|
StringIndexOutOfBoundsException |
if
offset
is negative or greater than the current
length()
.
|
---|
Inserts the string representation of the specified
float
value at
the specified
offset
. The
float
value is converted to a
string according to the rule defined by
valueOf(float)
.
offset | the index to insert at. |
---|---|
f |
the
float
value to insert.
|
StringIndexOutOfBoundsException |
if
offset
is negative or greater than the current
length()
.
|
---|
Inserts the string representation of the specified
char
value at
the specified
offset
. The
char
value is converted to a
string according to the rule defined by
valueOf(char)
.
offset | the index to insert at. |
---|---|
c |
the
char
value to insert.
|
IndexOutOfBoundsException |
if
offset
is negative or greater than the current
length()
.
|
---|
Inserts the string representation of the specified
char[]
at the
specified
offset
. The
char[]
value is converted to a
String according to the rule defined by
valueOf(char[])
.
offset | the index to insert at. |
---|---|
ch |
the
char[]
to insert.
|
StringIndexOutOfBoundsException |
if
offset
is negative or greater than the current
length()
.
|
---|
Inserts the string representation of the specified
CharSequence
at the specified
offset
. The
CharSequence
is converted
to a String as defined by
toString()
. If
s
is
null
, then the String
"null"
is inserted.
offset | the index to insert at. |
---|---|
s |
the
CharSequence
to insert.
|
IndexOutOfBoundsException |
if
offset
is negative or greater than the current
length()
.
|
---|
Inserts the string representation of the specified
boolean
value
at the specified
offset
. The
boolean
value is converted
to a string according to the rule defined by
valueOf(boolean)
.
offset | the index to insert at. |
---|---|
b |
the
boolean
value to insert.
|
StringIndexOutOfBoundsException |
if
offset
is negative or greater than the current
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. |
NullPointerException |
if
subString
is
null
.
|
---|
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.
IndexOutOfBoundsException |
if
index
is negative or greater than
length()
or if there aren't enough code points
before or after
index
to match
codePointOffset
.
|
---|
Replaces the specified subsequence in this builder with the specified string.
start | the inclusive begin index. |
---|---|
end | the exclusive end index. |
string | the replacement string. |
StringIndexOutOfBoundsException |
if
start
is negative, greater than the current
length()
or greater than
end
.
|
---|---|
NullPointerException |
if
str
is
null
.
|
Reverses the order of characters in this builder.
Sets the character at the
index
.
index | the zero-based index of the character to replace. |
---|---|
ch | the character to set. |
IndexOutOfBoundsException |
if
index
is negative or greater than or equal to the
current
length()
.
|
---|
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
.
length | the new length of this StringBuffer. |
---|
IndexOutOfBoundsException |
if
length < 0
.
|
---|
Returns a
CharSequence
of the subsequence from the
start
index to the
end
index.
start | the inclusive start index to begin the subsequence. |
---|---|
end | the exclusive end index to end the subsequence. |
IndexOutOfBoundsException |
if
start
is negative, greater than
end
or if
end
is greater than the current
length()
.
|
---|
Returns the String value of the subsequence from the
start
index
to the current end.
start | the inclusive start index to begin the subsequence. |
---|
StringIndexOutOfBoundsException |
if
start
is negative or greater than the current
length()
.
|
---|
Returns the String value of the subsequence from the
start
index
to the
end
index.
start | the inclusive start index to begin the subsequence. |
---|---|
end | the exclusive end index to end the subsequence. |
StringIndexOutOfBoundsException |
if
start
is negative, greater than
end
or if
end
is greater than the current
length()
.
|
---|
Returns the contents of this builder.
Trims off any extra capacity beyond the current length. Note, this method is NOT guaranteed to change the capacity of this object.