public final class StringBuilder extends Object implements Appendable, CharSequence, Serializable
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()
.
CharSequence
,
Appendable
,
StringBuffer
,
String
,
String.format(java.lang.String, java.lang.Object...)
,
Serialized FormConstructor and Description |
---|
StringBuilder()
Constructs an instance with an initial capacity of
16 . |
StringBuilder(CharSequence seq)
Constructs an instance that's initialized with the contents of the
specified
CharSequence . |
StringBuilder(int capacity)
Constructs an instance with the specified capacity.
|
StringBuilder(String str)
Constructs an instance that's initialized with the contents of the
specified
String . |
Modifier and Type | Method and Description |
---|---|
StringBuilder |
append(boolean b)
Appends the string representation of the specified
boolean value. |
StringBuilder |
append(char c)
Appends the string representation of the specified
char value. |
StringBuilder |
append(char[] chars)
Appends the string representation of the specified
char[] . |
StringBuilder |
append(char[] str,
int offset,
int len)
Appends the string representation of the specified subset of the
char[] . |
StringBuilder |
append(CharSequence csq)
Appends the string representation of the specified
CharSequence . |
StringBuilder |
append(CharSequence csq,
int start,
int end)
Appends the string representation of the specified subsequence of the
CharSequence . |
StringBuilder |
append(double d)
Appends the string representation of the specified
double value. |
StringBuilder |
append(float f)
Appends the string representation of the specified
float value. |
StringBuilder |
append(int i)
Appends the string representation of the specified
int value. |
StringBuilder |
append(long l)
Appends the string representation of the specified
long value. |
StringBuilder |
append(Object obj)
Appends the string representation of the specified
Object . |
StringBuilder |
append(String str)
Appends the contents of the specified string.
|
StringBuilder |
append(StringBuffer sb)
Appends the contents of the specified
StringBuffer . |
StringBuilder |
appendCodePoint(int codePoint)
Appends the encoded Unicode code point.
|
int |
capacity()
Returns the number of characters that can be held without growing.
|
char |
charAt(int index)
Retrieves the character at the
index . |
int |
codePointAt(int index)
Retrieves the Unicode code point value at the
index . |
int |
codePointBefore(int index)
Retrieves the Unicode code point value that precedes the
index . |
int |
codePointCount(int start,
int end)
Calculates the number of Unicode code points between
start
and end . |
StringBuilder |
delete(int start,
int end)
Deletes a sequence of characters specified by
start and end . |
StringBuilder |
deleteCharAt(int index)
Deletes the character at the specified index.
|
void |
ensureCapacity(int min)
Ensures that this object has a minimum capacity available before
requiring the internal buffer to be enlarged.
|
void |
getChars(int start,
int end,
char[] dst,
int dstStart)
Copies the requested sequence of characters into
dst passed
starting at dst . |
int |
indexOf(String string)
Searches for the first index of the specified character.
|
int |
indexOf(String subString,
int start)
Searches for the index of the specified character.
|
StringBuilder |
insert(int offset,
boolean b)
Inserts the string representation of the specified
boolean value
at the specified offset . |
StringBuilder |
insert(int offset,
char c)
Inserts the string representation of the specified
char value at
the specified offset . |
StringBuilder |
insert(int offset,
char[] ch)
Inserts the string representation of the specified
char[] at the
specified offset . |
StringBuilder |
insert(int offset,
char[] str,
int strOffset,
int strLen)
Inserts the string representation of the specified subsequence of the
char[] at the specified offset . |
StringBuilder |
insert(int offset,
CharSequence s)
Inserts the string representation of the specified
CharSequence
at the specified offset . |
StringBuilder |
insert(int offset,
CharSequence s,
int start,
int end)
Inserts the string representation of the specified subsequence of the
CharSequence at the specified offset . |
StringBuilder |
insert(int offset,
double d)
Inserts the string representation of the specified
double value
at the specified offset . |
StringBuilder |
insert(int offset,
float f)
Inserts the string representation of the specified
float value at
the specified offset . |
StringBuilder |
insert(int offset,
int i)
Inserts the string representation of the specified
int value at
the specified offset . |
StringBuilder |
insert(int offset,
long l)
Inserts the string representation of the specified
long value at
the specified offset . |
StringBuilder |
insert(int offset,
Object obj)
Inserts the string representation of the specified
Object at the
specified offset . |
StringBuilder |
insert(int offset,
String str)
Inserts the specified string at the specified
offset . |
int |
lastIndexOf(String string)
Searches for the last index of the specified character.
|
int |
lastIndexOf(String subString,
int start)
Searches for the index of the specified character.
|
int |
length()
The current length.
|
int |
offsetByCodePoints(int index,
int codePointOffset)
Returns the index that is offset
codePointOffset code points from
index . |
StringBuilder |
replace(int start,
int end,
String string)
Replaces the specified subsequence in this builder with the specified
string.
|
StringBuilder |
reverse()
Reverses the order of characters in this builder.
|
void |
setCharAt(int index,
char ch)
Sets the character at the
index . |
void |
setLength(int length)
Sets the current length to a new value.
|
CharSequence |
subSequence(int start,
int end)
Returns a
CharSequence of the subsequence from the start
index to the end index. |
String |
substring(int start)
Returns the String value of the subsequence from the
start index
to the current end. |
String |
substring(int start,
int end)
Returns the String value of the subsequence from the
start index
to the end index. |
String |
toString()
Returns the contents of this builder.
|
void |
trimToSize()
Trims off any extra capacity beyond the current length.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
charAt, length, subSequence
public StringBuilder()
16
.capacity()
public StringBuilder(int capacity)
capacity
- the initial capacity to use.NegativeArraySizeException
- if the specified capacity
is negative.capacity()
public StringBuilder(CharSequence seq)
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
.public StringBuilder(String str)
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
.public StringBuilder append(boolean b)
boolean
value.
The boolean
value is converted to a String according to the rule
defined by String.valueOf(boolean)
.b
- the boolean
value to append.String.valueOf(boolean)
public StringBuilder append(char c)
char
value.
The char
value is converted to a string according to the rule
defined by String.valueOf(char)
.append
in interface Appendable
c
- the char
value to append.String.valueOf(char)
public StringBuilder append(int i)
int
value. The
int
value is converted to a string according to the rule defined
by String.valueOf(int)
.i
- the int
value to append.String.valueOf(int)
public StringBuilder append(long l)
long
value.
The long
value is converted to a string according to the rule
defined by String.valueOf(long)
.l
- the long
value.String.valueOf(long)
public StringBuilder append(float f)
float
value.
The float
value is converted to a string according to the rule
defined by String.valueOf(float)
.f
- the float
value to append.String.valueOf(float)
public StringBuilder append(double d)
double
value.
The double
value is converted to a string according to the rule
defined by String.valueOf(double)
.d
- the double
value to append.String.valueOf(double)
public StringBuilder append(Object obj)
Object
.
The Object
value is converted to a string according to the rule
defined by String.valueOf(Object)
.obj
- the Object
to append.String.valueOf(Object)
public StringBuilder append(String str)
null
, then the string "null"
is appended.str
- the string to append.public StringBuilder append(StringBuffer sb)
StringBuffer
. If the
StringBuffer is null
, then the string "null"
is
appended.sb
- the StringBuffer
to append.public StringBuilder append(char[] chars)
char[]
.
The char[]
is converted to a string according to the rule
defined by String.valueOf(char[])
.chars
- the char[]
to append..String.valueOf(char[])
public StringBuilder append(char[] str, int offset, int len)
char[]
. The char[]
value is converted to a String according to
the rule defined by String.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.String.valueOf(char[],int,int)
public StringBuilder append(CharSequence csq)
CharSequence
.
If the CharSequence
is null
, then the string "null"
is appended.append
in interface Appendable
csq
- the CharSequence
to append.public StringBuilder append(CharSequence csq, int start, int end)
CharSequence
. If the CharSequence
is null
, then
the string "null"
is used to extract the subsequence from.append
in interface Appendable
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
.public StringBuilder appendCodePoint(int codePoint)
char[]
as defined by Character.toChars(int)
.codePoint
- the Unicode code point to encode and append.Character.toChars(int)
public StringBuilder delete(int start, int end)
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
.public StringBuilder deleteCharAt(int index)
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.public StringBuilder insert(int offset, boolean b)
boolean
value
at the specified offset
. The boolean
value is converted
to a string according to the rule defined by
String.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
.String.valueOf(boolean)
public StringBuilder insert(int offset, char c)
char
value at
the specified offset
. The char
value is converted to a
string according to the rule defined by String.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()
.String.valueOf(char)
public StringBuilder insert(int offset, int i)
int
value at
the specified offset
. The int
value is converted to a
String according to the rule defined by String.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()
.String.valueOf(int)
public StringBuilder insert(int offset, long l)
long
value at
the specified offset
. The long
value is converted to a
String according to the rule defined by String.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()}.String.valueOf(long)
public StringBuilder insert(int offset, float f)
float
value at
the specified offset
. The float
value is converted to a
string according to the rule defined by String.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()
.String.valueOf(float)
public StringBuilder insert(int offset, double d)
double
value
at the specified offset
. The double
value is converted
to a String according to the rule defined by
String.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()
.String.valueOf(double)
public StringBuilder insert(int offset, Object obj)
Object
at the
specified offset
. The Object
value is converted to a
String according to the rule defined by String.valueOf(Object)
.offset
- the index to insert at.obj
- the Object
to insert.StringIndexOutOfBoundsException
- if offset
is negative or greater than the current
length()
.String.valueOf(Object)
public StringBuilder insert(int offset, String str)
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()
.public StringBuilder insert(int offset, char[] ch)
char[]
at the
specified offset
. The char[]
value is converted to a
String according to the rule defined by String.valueOf(char[])
.offset
- the index to insert at.ch
- the char[]
to insert.StringIndexOutOfBoundsException
- if offset
is negative or greater than the current
length()
.String.valueOf(char[])
public StringBuilder insert(int offset, char[] str, int strOffset, int strLen)
char[]
at the specified offset
. The char[]
value
is converted to a String according to the rule defined by
String.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.String.valueOf(char[],int,int)
public StringBuilder insert(int offset, CharSequence s)
CharSequence
at the specified offset
. The CharSequence
is converted
to a String as defined by CharSequence.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()
.CharSequence.toString()
public StringBuilder insert(int offset, CharSequence s, int start, int end)
CharSequence
at the specified offset
. The CharSequence
is converted to a String as defined by
CharSequence.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.CharSequence.subSequence(int, int)
public StringBuilder replace(int start, int end, String 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
.public StringBuilder reverse()
public String toString()
toString
in interface CharSequence
public int capacity()
ensureCapacity(int)
,
length()
public char charAt(int index)
index
.index
- the index of the character to retrieve.IndexOutOfBoundsException
- if index
is negative or greater than or equal to the
current length()
.public void ensureCapacity(int min)
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.public void getChars(int start, int end, char[] dst, int dstStart)
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
.public int length()
public void setCharAt(int index, char ch)
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()
.public void setLength(int length)
char
value of
.length
- the new length of this StringBuffer.IndexOutOfBoundsException
- if length < 0
.length()
public String substring(int start)
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()
.public String substring(int start, int end)
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()
.public CharSequence subSequence(int start, int end)
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()
.public int indexOf(String string)
string
- the string to find.lastIndexOf(String)
public int indexOf(String subString, int start)
subString
- the string to find.start
- the starting offset.lastIndexOf(String,int)
public int lastIndexOf(String string)
string
- the string to find.NullPointerException
- if string
is null
.String.lastIndexOf(java.lang.String)
public int lastIndexOf(String subString, int start)
subString
- the string to find.start
- the starting offset.NullPointerException
- if subString
is null
.String.lastIndexOf(String,int)
public void trimToSize()
public int codePointAt(int index)
index
.index
- the index to the char
code unit.IndexOutOfBoundsException
- if index
is negative or greater than or equal to
length()
.Character
,
Character.codePointAt(char[], int, int)
public int codePointBefore(int index)
index
.index
- the index to the char
code unit within this object.IndexOutOfBoundsException
- if index
is less than 1 or greater than
length()
.Character
,
Character.codePointBefore(char[], int, int)
public int codePointCount(int start, int end)
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()
.Character
,
Character.codePointCount(char[], int, int)
public int offsetByCodePoints(int index, int codePointOffset)
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
.Character
,
Character.offsetByCodePoints(char[], int, int, int, int)