Package java.lang
Class StringBuilder
java.lang.Object
java.lang.StringBuilder
- All Implemented Interfaces:
Serializable,Appendable,CharSequence
public final class StringBuilder extends Object implements Appendable, CharSequence, Serializable
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().
- Since:
- 1.5
- See Also:
CharSequence,Appendable,StringBuffer,String,String.format(java.lang.String, java.lang.Object...), Serialized Form
-
Constructor Summary
Constructors Constructor Description StringBuilder()Constructs an instance with an initial capacity of16.StringBuilder(int capacity)Constructs an instance with the specified capacity.StringBuilder(CharSequence seq)Constructs an instance that's initialized with the contents of the specifiedCharSequence.StringBuilder(String str)Constructs an instance that's initialized with the contents of the specifiedString. -
Method Summary
Modifier and Type Method Description StringBuilderappend(boolean b)Appends the string representation of the specifiedbooleanvalue.StringBuilderappend(char c)Appends the string representation of the specifiedcharvalue.StringBuilderappend(char[] chars)Appends the string representation of the specifiedchar[].StringBuilderappend(char[] str, int offset, int len)Appends the string representation of the specified subset of thechar[].StringBuilderappend(double d)Appends the string representation of the specifieddoublevalue.StringBuilderappend(float f)Appends the string representation of the specifiedfloatvalue.StringBuilderappend(int i)Appends the string representation of the specifiedintvalue.StringBuilderappend(long l)Appends the string representation of the specifiedlongvalue.StringBuilderappend(CharSequence csq)Appends the string representation of the specifiedCharSequence.StringBuilderappend(CharSequence csq, int start, int end)Appends the string representation of the specified subsequence of theCharSequence.StringBuilderappend(Object obj)Appends the string representation of the specifiedObject.StringBuilderappend(String str)Appends the contents of the specified string.StringBuilderappend(StringBuffer sb)Appends the contents of the specifiedStringBuffer.StringBuilderappendCodePoint(int codePoint)Appends the encoded Unicode code point.intcapacity()Returns the number of characters that can be held without growing.charcharAt(int index)Retrieves the character at theindex.intcodePointAt(int index)Retrieves the Unicode code point value at theindex.intcodePointBefore(int index)Retrieves the Unicode code point value that precedes theindex.intcodePointCount(int start, int end)Calculates the number of Unicode code points betweenstartandend.StringBuilderdelete(int start, int end)Deletes a sequence of characters specified bystartandend.StringBuilderdeleteCharAt(int index)Deletes the character at the specified index.voidensureCapacity(int min)Ensures that this object has a minimum capacity available before requiring the internal buffer to be enlarged.voidgetChars(int start, int end, char[] dst, int dstStart)Copies the requested sequence of characters intodstpassed starting atdst.intindexOf(String string)Searches for the first index of the specified character.intindexOf(String subString, int start)Searches for the index of the specified character.StringBuilderinsert(int offset, boolean b)Inserts the string representation of the specifiedbooleanvalue at the specifiedoffset.StringBuilderinsert(int offset, char c)Inserts the string representation of the specifiedcharvalue at the specifiedoffset.StringBuilderinsert(int offset, char[] ch)Inserts the string representation of the specifiedchar[]at the specifiedoffset.StringBuilderinsert(int offset, char[] str, int strOffset, int strLen)Inserts the string representation of the specified subsequence of thechar[]at the specifiedoffset.StringBuilderinsert(int offset, double d)Inserts the string representation of the specifieddoublevalue at the specifiedoffset.StringBuilderinsert(int offset, float f)Inserts the string representation of the specifiedfloatvalue at the specifiedoffset.StringBuilderinsert(int offset, int i)Inserts the string representation of the specifiedintvalue at the specifiedoffset.StringBuilderinsert(int offset, long l)Inserts the string representation of the specifiedlongvalue at the specifiedoffset.StringBuilderinsert(int offset, CharSequence s)Inserts the string representation of the specifiedCharSequenceat the specifiedoffset.StringBuilderinsert(int offset, CharSequence s, int start, int end)Inserts the string representation of the specified subsequence of theCharSequenceat the specifiedoffset.StringBuilderinsert(int offset, Object obj)Inserts the string representation of the specifiedObjectat the specifiedoffset.StringBuilderinsert(int offset, String str)Inserts the specified string at the specifiedoffset.intlastIndexOf(String string)Searches for the last index of the specified character.intlastIndexOf(String subString, int start)Searches for the index of the specified character.intlength()The current length.intoffsetByCodePoints(int index, int codePointOffset)Returns the index that is offsetcodePointOffsetcode points fromindex.StringBuilderreplace(int start, int end, String string)Replaces the specified subsequence in this builder with the specified string.StringBuilderreverse()Reverses the order of characters in this builder.voidsetCharAt(int index, char ch)Sets the character at theindex.voidsetLength(int length)Sets the current length to a new value.CharSequencesubSequence(int start, int end)Returns aCharSequenceof the subsequence from thestartindex to theendindex.Stringsubstring(int start)Returns the String value of the subsequence from thestartindex to the current end.Stringsubstring(int start, int end)Returns the String value of the subsequence from thestartindex to theendindex.StringtoString()Returns the contents of this builder.voidtrimToSize()Trims off any extra capacity beyond the current length.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.CharSequence
charAt, length, subSequence
-
Constructor Details
-
StringBuilder
public StringBuilder()Constructs an instance with an initial capacity of16.- See Also:
capacity()
-
StringBuilder
public StringBuilder(int capacity)Constructs an instance with the specified capacity.- Parameters:
capacity- the initial capacity to use.- Throws:
NegativeArraySizeException- if the specifiedcapacityis negative.- See Also:
capacity()
-
StringBuilder
Constructs an instance that's initialized with the contents of the specifiedCharSequence. The capacity of the new builder will be the length of theCharSequenceplus 16.- Parameters:
seq- theCharSequenceto copy into the builder.- Throws:
NullPointerException- ifseqisnull.
-
StringBuilder
Constructs an instance that's initialized with the contents of the specifiedString. The capacity of the new builder will be the length of theStringplus 16.- Parameters:
str- theStringto copy into the builder.- Throws:
NullPointerException- ifstrisnull.
-
-
Method Details
-
append
Appends the string representation of the specifiedbooleanvalue. Thebooleanvalue is converted to a String according to the rule defined byString.valueOf(boolean).- Parameters:
b- thebooleanvalue to append.- Returns:
- this builder.
- See Also:
String.valueOf(boolean)
-
append
Appends the string representation of the specifiedcharvalue. Thecharvalue is converted to a string according to the rule defined byString.valueOf(char).- Specified by:
appendin interfaceAppendable- Parameters:
c- thecharvalue to append.- Returns:
- this builder.
- See Also:
String.valueOf(char)
-
append
Appends the string representation of the specifiedintvalue. Theintvalue is converted to a string according to the rule defined byString.valueOf(int).- Parameters:
i- theintvalue to append.- Returns:
- this builder.
- See Also:
String.valueOf(int)
-
append
Appends the string representation of the specifiedlongvalue. Thelongvalue is converted to a string according to the rule defined byString.valueOf(long).- Parameters:
l- thelongvalue.- Returns:
- this builder.
- See Also:
String.valueOf(long)
-
append
Appends the string representation of the specifiedfloatvalue. Thefloatvalue is converted to a string according to the rule defined byString.valueOf(float).- Parameters:
f- thefloatvalue to append.- Returns:
- this builder.
- See Also:
String.valueOf(float)
-
append
Appends the string representation of the specifieddoublevalue. Thedoublevalue is converted to a string according to the rule defined byString.valueOf(double).- Parameters:
d- thedoublevalue to append.- Returns:
- this builder.
- See Also:
String.valueOf(double)
-
append
Appends the string representation of the specifiedObject. TheObjectvalue is converted to a string according to the rule defined byString.valueOf(Object).- Parameters:
obj- theObjectto append.- Returns:
- this builder.
- See Also:
String.valueOf(Object)
-
append
Appends the contents of the specified string. If the string isnull, then the string"null"is appended.- Parameters:
str- the string to append.- Returns:
- this builder.
-
append
Appends the contents of the specifiedStringBuffer. If the StringBuffer isnull, then the string"null"is appended.- Parameters:
sb- theStringBufferto append.- Returns:
- this builder.
-
append
Appends the string representation of the specifiedchar[]. Thechar[]is converted to a string according to the rule defined byString.valueOf(char[]).- Parameters:
chars- thechar[]to append..- Returns:
- this builder.
- See Also:
String.valueOf(char[])
-
append
Appends the string representation of the specified subset of thechar[]. Thechar[]value is converted to a String according to the rule defined byString.valueOf(char[],int,int).- Parameters:
str- thechar[]to append.offset- the inclusive offset index.len- the number of characters.- Returns:
- this builder.
- Throws:
ArrayIndexOutOfBoundsException- ifoffsetandlendo not specify a valid subsequence.- See Also:
String.valueOf(char[],int,int)
-
append
Appends the string representation of the specifiedCharSequence. If theCharSequenceisnull, then the string"null"is appended.- Specified by:
appendin interfaceAppendable- Parameters:
csq- theCharSequenceto append.- Returns:
- this builder.
-
append
Appends the string representation of the specified subsequence of theCharSequence. If theCharSequenceisnull, then the string"null"is used to extract the subsequence from.- Specified by:
appendin interfaceAppendable- Parameters:
csq- theCharSequenceto append.start- the beginning index.end- the ending index.- Returns:
- this builder.
- Throws:
IndexOutOfBoundsException- ifstartorendare negative,startis greater thanendorendis greater than the length ofcsq.
-
appendCodePoint
Appends the encoded Unicode code point. The code point is converted to achar[]as defined byCharacter.toChars(int).- Parameters:
codePoint- the Unicode code point to encode and append.- Returns:
- this builder.
- See Also:
Character.toChars(int)
-
delete
Deletes a sequence of characters specified bystartandend. Shifts any remaining characters to the left.- Parameters:
start- the inclusive start index.end- the exclusive end index.- Returns:
- this builder.
- Throws:
StringIndexOutOfBoundsException- ifstartis less than zero, greater than the current length or greater thanend.
-
deleteCharAt
Deletes the character at the specified index. shifts any remaining characters to the left.- Parameters:
index- the index of the character to delete.- Returns:
- this builder.
- Throws:
StringIndexOutOfBoundsException- ifindexis less than zero or is greater than or equal to the current length.
-
insert
Inserts the string representation of the specifiedbooleanvalue at the specifiedoffset. Thebooleanvalue is converted to a string according to the rule defined byString.valueOf(boolean).- Parameters:
offset- the index to insert at.b- thebooleanvalue to insert.- Returns:
- this builder.
- Throws:
StringIndexOutOfBoundsException- ifoffsetis negative or greater than the currentlength.- See Also:
String.valueOf(boolean)
-
insert
Inserts the string representation of the specifiedcharvalue at the specifiedoffset. Thecharvalue is converted to a string according to the rule defined byString.valueOf(char).- Parameters:
offset- the index to insert at.c- thecharvalue to insert.- Returns:
- this builder.
- Throws:
IndexOutOfBoundsException- ifoffsetis negative or greater than the currentlength().- See Also:
String.valueOf(char)
-
insert
Inserts the string representation of the specifiedintvalue at the specifiedoffset. Theintvalue is converted to a String according to the rule defined byString.valueOf(int).- Parameters:
offset- the index to insert at.i- theintvalue to insert.- Returns:
- this builder.
- Throws:
StringIndexOutOfBoundsException- ifoffsetis negative or greater than the currentlength().- See Also:
String.valueOf(int)
-
insert
Inserts the string representation of the specifiedlongvalue at the specifiedoffset. Thelongvalue is converted to a String according to the rule defined byString.valueOf(long).- Parameters:
offset- the index to insert at.l- thelongvalue to insert.- Returns:
- this builder.
- Throws:
StringIndexOutOfBoundsException- ifoffsetis negative or greater than the current {code length()}.- See Also:
String.valueOf(long)
-
insert
Inserts the string representation of the specifiedfloatvalue at the specifiedoffset. Thefloatvalue is converted to a string according to the rule defined byString.valueOf(float).- Parameters:
offset- the index to insert at.f- thefloatvalue to insert.- Returns:
- this builder.
- Throws:
StringIndexOutOfBoundsException- ifoffsetis negative or greater than the currentlength().- See Also:
String.valueOf(float)
-
insert
Inserts the string representation of the specifieddoublevalue at the specifiedoffset. Thedoublevalue is converted to a String according to the rule defined byString.valueOf(double).- Parameters:
offset- the index to insert at.d- thedoublevalue to insert.- Returns:
- this builder.
- Throws:
StringIndexOutOfBoundsException- ifoffsetis negative or greater than the currentlength().- See Also:
String.valueOf(double)
-
insert
Inserts the string representation of the specifiedObjectat the specifiedoffset. TheObjectvalue is converted to a String according to the rule defined byString.valueOf(Object).- Parameters:
offset- the index to insert at.obj- theObjectto insert.- Returns:
- this builder.
- Throws:
StringIndexOutOfBoundsException- ifoffsetis negative or greater than the currentlength().- See Also:
String.valueOf(Object)
-
insert
Inserts the specified string at the specifiedoffset. If the specified string is null, then the String"null"is inserted.- Parameters:
offset- the index to insert at.str- theStringto insert.- Returns:
- this builder.
- Throws:
StringIndexOutOfBoundsException- ifoffsetis negative or greater than the currentlength().
-
insert
Inserts the string representation of the specifiedchar[]at the specifiedoffset. Thechar[]value is converted to a String according to the rule defined byString.valueOf(char[]).- Parameters:
offset- the index to insert at.ch- thechar[]to insert.- Returns:
- this builder.
- Throws:
StringIndexOutOfBoundsException- ifoffsetis negative or greater than the currentlength().- See Also:
String.valueOf(char[])
-
insert
Inserts the string representation of the specified subsequence of thechar[]at the specifiedoffset. Thechar[]value is converted to a String according to the rule defined byString.valueOf(char[],int,int).- Parameters:
offset- the index to insert at.str- thechar[]to insert.strOffset- the inclusive index.strLen- the number of characters.- Returns:
- this builder.
- Throws:
StringIndexOutOfBoundsException- ifoffsetis negative or greater than the currentlength(), orstrOffsetandstrLendo not specify a valid subsequence.- See Also:
String.valueOf(char[],int,int)
-
insert
Inserts the string representation of the specifiedCharSequenceat the specifiedoffset. TheCharSequenceis converted to a String as defined byCharSequence.toString(). Ifsisnull, then the String"null"is inserted.- Parameters:
offset- the index to insert at.s- theCharSequenceto insert.- Returns:
- this builder.
- Throws:
IndexOutOfBoundsException- ifoffsetis negative or greater than the currentlength().- See Also:
CharSequence.toString()
-
insert
Inserts the string representation of the specified subsequence of theCharSequenceat the specifiedoffset. TheCharSequenceis converted to a String as defined byCharSequence.subSequence(int, int). If theCharSequenceisnull, then the string"null"is used to determine the subsequence.- Parameters:
offset- the index to insert at.s- theCharSequenceto insert.start- the start of the subsequence of the character sequence.end- the end of the subsequence of the character sequence.- Returns:
- this builder.
- Throws:
IndexOutOfBoundsException- ifoffsetis negative or greater than the currentlength(), orstartandenddo not specify a valid subsequence.- See Also:
CharSequence.subSequence(int, int)
-
replace
Replaces the specified subsequence in this builder with the specified string.- Parameters:
start- the inclusive begin index.end- the exclusive end index.string- the replacement string.- Returns:
- this builder.
- Throws:
StringIndexOutOfBoundsException- ifstartis negative, greater than the currentlength()or greater thanend.NullPointerException- ifstrisnull.
-
reverse
Reverses the order of characters in this builder.- Returns:
- this buffer.
-
toString
Returns the contents of this builder.- Specified by:
toStringin interfaceCharSequence- Returns:
- the string representation of the data in this builder.
-
capacity
public int capacity()Returns the number of characters that can be held without growing.- Returns:
- the capacity
- See Also:
ensureCapacity(int),length()
-
charAt
public char charAt(int index)Retrieves the character at theindex.- Parameters:
index- the index of the character to retrieve.- Returns:
- the char value.
- Throws:
IndexOutOfBoundsException- ifindexis negative or greater than or equal to the currentlength().
-
ensureCapacity
public void ensureCapacity(int min)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 theminimumCapacityis larger than the currentcapacity(), then the capacity will be increased to the largest value of either theminimumCapacityor the current capacity multiplied by two plus two. Although this is the general policy, there is no guarantee that the capacity will change.- Parameters:
min- the new minimum capacity to set.
-
getChars
public void getChars(int start, int end, char[] dst, int dstStart)Copies the requested sequence of characters intodstpassed starting atdst.- Parameters:
start- the inclusive start index of the characters to copy.end- the exclusive end index of the characters to copy.dst- thechar[]to copy the characters to.dstStart- the inclusive start index ofdstto begin copying to.- Throws:
IndexOutOfBoundsException- if thestartis negative, thedstStartis negative, thestartis greater thanend, theendis greater than the currentlength()ordstStart + end - beginis greater thandst.length.
-
length
public int length()The current length.- Returns:
- the number of characters contained in this instance.
-
setCharAt
public void setCharAt(int index, char ch)Sets the character at theindex.- Parameters:
index- the zero-based index of the character to replace.ch- the character to set.- Throws:
IndexOutOfBoundsException- ifindexis negative or greater than or equal to the currentlength().
-
setLength
public void setLength(int 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 thecharvalue of
-