Class StringBuffer
- All Implemented Interfaces:
Serializable,Appendable,CharSequence
public final class StringBuffer extends Object implements Appendable, Serializable, CharSequence
sequence of characters for use in creating
strings, where all accesses are synchronized. This class has mostly been replaced
by StringBuilder because this synchronization is rarely useful. This
class is mainly used to interact with legacy APIs that expose it.
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 StringBuffer("a").append("b").append("c").toString().
- Since:
- 1.0
- See Also:
CharSequence,Appendable,StringBuilder,String,String.format(java.lang.String, java.lang.Object...), Serialized Form
-
Constructor Summary
Constructors Constructor Description StringBuffer()Constructs a new StringBuffer using the default capacity which is 16.StringBuffer(int capacity)Constructs a new StringBuffer using the specified capacity.StringBuffer(CharSequence cs)Constructs a StringBuffer and initializes it with the content from the specifiedCharSequence.StringBuffer(String string)Constructs a new StringBuffer containing the characters in the specified string. -
Method Summary
Modifier and Type Method Description StringBufferappend(boolean b)Adds the string representation of the specified boolean to the end of this StringBuffer.StringBufferappend(char ch)Adds the specified character to the end of this buffer.StringBufferappend(char[] chars)Adds the character array to the end of this buffer.StringBufferappend(char[] chars, int start, int length)Adds the specified sequence of characters to the end of this buffer.StringBufferappend(double d)Adds the string representation of the specified double to the end of this StringBuffer.StringBufferappend(float f)Adds the string representation of the specified float to the end of this StringBuffer.StringBufferappend(int i)Adds the string representation of the specified integer to the end of this StringBuffer.StringBufferappend(long l)Adds the string representation of the specified long to the end of this StringBuffer.StringBufferappend(CharSequence s)Appends the specified CharSequence to this buffer.StringBufferappend(CharSequence s, int start, int end)Appends the specified subsequence of the CharSequence to this buffer.StringBufferappend(Object obj)Adds the string representation of the specified object to the end of this StringBuffer.StringBufferappend(String string)Adds the specified string to the end of this buffer.StringBufferappend(StringBuffer sb)Adds the specified StringBuffer to the end of this buffer.StringBufferappendCodePoint(int codePoint)Appends the string representation of the specified Unicode code point to the end of this buffer.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 beginIndex, int endIndex)Calculates the number of Unicode code points betweenstartandend.StringBufferdelete(int start, int end)Deletes a range of characters.StringBufferdeleteCharAt(int location)Deletes the character at the specified offset.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[] buffer, int idx)Copies the requested sequence of characters to thechar[]passed starting atidx.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.StringBufferinsert(int index, boolean b)Inserts the string representation of the specified boolean into this buffer at the specified offset.StringBufferinsert(int index, char ch)Inserts the character into this buffer at the specified offset.StringBufferinsert(int index, char[] chars)Inserts the character array into this buffer at the specified offset.StringBufferinsert(int index, char[] chars, int start, int length)Inserts the specified subsequence of characters into this buffer at the specified index.StringBufferinsert(int index, double d)Inserts the string representation of the specified into this buffer double at the specified offset.StringBufferinsert(int index, float f)Inserts the string representation of the specified float into this buffer at the specified offset.StringBufferinsert(int index, int i)Inserts the string representation of the specified integer into this buffer at the specified offset.StringBufferinsert(int index, long l)Inserts the string representation of the specified long into this buffer at the specified offset.StringBufferinsert(int index, CharSequence s)Inserts the specified CharSequence into this buffer at the specified index.StringBufferinsert(int index, CharSequence s, int start, int end)Inserts the specified subsequence into this buffer at the specified index.StringBufferinsert(int index, Object obj)Inserts the string representation of the specified object into this buffer at the specified offset.StringBufferinsert(int index, String string)Inserts the string into this buffer at the specified offset.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.StringBufferreplace(int start, int end, String string)Replaces the characters in the specified range with the contents of the specified string.StringBufferreverse()Reverses the order of characters in this buffer.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 current String representation.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
length
-
Constructor Details
-
StringBuffer
public StringBuffer()Constructs a new StringBuffer using the default capacity which is 16. -
StringBuffer
public StringBuffer(int capacity)Constructs a new StringBuffer using the specified capacity.- Parameters:
capacity- the initial capacity.
-
StringBuffer
Constructs a new StringBuffer containing the characters in the specified string. The capacity of the new buffer will be the length of theStringplus the default capacity.- Parameters:
string- the string content with which to initialize the new instance.- Throws:
NullPointerException- ifstringisnull.
-
StringBuffer
Constructs a StringBuffer and initializes it with the content from the specifiedCharSequence. The capacity of the new buffer will be the length of theCharSequenceplus the default capacity.- Parameters:
cs- the content to initialize the instance.- Throws:
NullPointerException- ifcsisnull.- Since:
- 1.5
-
-
Method Details
-
append
Adds the string representation of the specified boolean to the end of this StringBuffer.If the argument is
truethe string"true"is appended, otherwise the string"false"is appended.- Parameters:
b- the boolean to append.- Returns:
- this StringBuffer.
- See Also:
String.valueOf(boolean)
-
append
Adds the specified character to the end of this buffer.- Specified by:
appendin interfaceAppendable- Parameters:
ch- the character to append.- Returns:
- this StringBuffer.
- See Also:
String.valueOf(char)
-
append
Adds the string representation of the specified double to the end of this StringBuffer.- Parameters:
d- the double to append.- Returns:
- this StringBuffer.
- See Also:
String.valueOf(double)
-
append
Adds the string representation of the specified float to the end of this StringBuffer.- Parameters:
f- the float to append.- Returns:
- this StringBuffer.
- See Also:
String.valueOf(float)
-
append
Adds the string representation of the specified integer to the end of this StringBuffer.- Parameters:
i- the integer to append.- Returns:
- this StringBuffer.
- See Also:
String.valueOf(int)
-
append
Adds the string representation of the specified long to the end of this StringBuffer.- Parameters:
l- the long to append.- Returns:
- this StringBuffer.
- See Also:
String.valueOf(long)
-
append
Adds the string representation of the specified object to the end of this StringBuffer.If the specified object is
nullthe string"null"is appended, otherwise the objectstoStringis used to get its string representation.- Parameters:
obj- the object to append (may be null).- Returns:
- this StringBuffer.
- See Also:
String.valueOf(Object)
-
append
Adds the specified string to the end of this buffer.If the specified string is
nullthe string"null"is appended, otherwise the contents of the specified string is appended.- Parameters:
string- the string to append (may be null).- Returns:
- this StringBuffer.
-
append
Adds the specified StringBuffer to the end of this buffer.If the specified StringBuffer is
nullthe string"null"is appended, otherwise the contents of the specified StringBuffer is appended.- Parameters:
sb- the StringBuffer to append (may be null).- Returns:
- this StringBuffer.
- Since:
- 1.4
-
append
Adds the character array to the end of this buffer.- Parameters:
chars- the character array to append.- Returns:
- this StringBuffer.
- Throws:
NullPointerException- ifcharsisnull.
-
append
Adds the specified sequence of characters to the end of this buffer.- Parameters:
chars- the character array to append.start- the starting offset.length- the number of characters.- Returns:
- this StringBuffer.
- Throws:
ArrayIndexOutOfBoundsException- iflength < 0,start < 0orstart + length > chars.length.NullPointerException- ifcharsisnull.
-
append
Appends the specified CharSequence to this buffer.If the specified CharSequence is
nullthe string"null"is appended, otherwise the contents of the specified CharSequence is appended.- Specified by:
appendin interfaceAppendable- Parameters:
s- the CharSequence to append.- Returns:
- this StringBuffer.
- Since:
- 1.5
-
append
Appends the specified subsequence of the CharSequence to this buffer.If the specified CharSequence is
null, then the string"null"is used to extract a subsequence.- Specified by:
appendin interfaceAppendable- Parameters:
s- the CharSequence to append.start- the inclusive start index.end- the exclusive end index.- Returns:
- this StringBuffer.
- Throws:
IndexOutOfBoundsException- ifstartorendare negative,startis greater thanendorendis greater than the length ofs.- Since:
- 1.5
-
appendCodePoint
Appends the string representation of the specified Unicode code point to the end of this buffer.The code point is converted to a
char[]as defined byCharacter.toChars(int).- Parameters:
codePoint- the Unicode code point to encode and append.- Returns:
- this StringBuffer.
- Since:
- 1.5
- See Also:
Character.toChars(int)
-
charAt
public char charAt(int index)Retrieves the character at theindex.- Specified by:
charAtin interfaceCharSequence- Parameters:
index- the index of the character to retrieve.- Returns:
- the char value.
-
codePointAt
public int codePointAt(int index)Retrieves the Unicode code point value at theindex.- Parameters:
index- the index to thecharcode unit.- Returns:
- the Unicode code point value.
- See Also:
Character,Character.codePointAt(char[], int, int)
-
codePointBefore
public int codePointBefore(int index)Retrieves the Unicode code point value that precedes theindex.- Parameters:
index- the index to thecharcode unit within this object.- Returns:
- the Unicode code point value.
- See Also:
Character,Character.codePointBefore(char[], int, int)
-
codePointCount
public int codePointCount(int beginIndex, int endIndex)Calculates the number of Unicode code points betweenstartandend.- Parameters:
beginIndex- the inclusive beginning index of the subsequence.endIndex- the exclusive end index of the subsequence.- Returns:
- the number of Unicode code points in the subsequence.
- See Also:
Character,Character.codePointCount(char[], int, int)
-
delete
Deletes a range of characters.- Parameters:
start- the offset of the first character.end- the offset one past the last character.- Returns:
- this StringBuffer.
- Throws:
StringIndexOutOfBoundsException- ifstart < 0,start > endorend > length().
-
deleteCharAt
Deletes the character at the specified offset.- Parameters:
location- the offset of the character to delete.- Returns:
- this StringBuffer.
- Throws:
StringIndexOutOfBoundsException- iflocation < 0orlocation >= length()
-
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[] buffer, int idx)Copies the requested sequence of characters to thechar[]passed starting atidx.- Parameters:
start- the starting offset of characters to copy.end- the ending offset of characters to copy.buffer- the destination character array.idx- the starting offset in the character array.- Throws:
IndexOutOfBoundsException- ifstart < 0,end > length(),start > end,index < 0,end - start > buffer.length - index
-
indexOf
Searches for the index of the specified character. The search for the character starts at the specified offset and moves towards the end.- Parameters:
subString- the string to find.start- the starting offset.- Returns:
- the index of the specified character, -1 if the character isn't found
- See Also:
lastIndexOf(String,int)
-
insert
Inserts the character into this buffer at the specified offset.- Parameters:
index- the index at which to insert.ch- the character to insert.- Returns:
- this buffer.
- Throws:
ArrayIndexOutOfBoundsException- ifindex < 0orindex > length().
-
insert
Inserts the string representation of the specified boolean into this buffer at the specified offset.- Parameters:
index- the index at which to insert.b- the boolean to insert.- Returns:
- this buffer.
- Throws:
StringIndexOutOfBoundsException- ifindex < 0orindex > length().
-
insert
Inserts the string representation of the specified integer into this buffer at the specified offset.- Parameters:
index- the index at which to insert.i- the integer to insert.- Returns:
- this buffer.
- Throws:
StringIndexOutOfBoundsException- ifindex < 0orindex > length().
-
insert
Inserts the string representation of the specified long into this buffer at the specified offset.- Parameters:
index- the index at which to insert.l- the long to insert.- Returns:
- this buffer.
- Throws:
StringIndexOutOfBoundsException- ifindex < 0orindex > length().
-
insert
Inserts the string representation of the specified into this buffer double at the specified offset.- Parameters:
index- the index at which to insert.d- the double to insert.- Returns:
- this buffer.
- Throws:
StringIndexOutOfBoundsException- ifindex < 0orindex > length().
-
insert
Inserts the string representation of the specified float into this buffer at the specified offset.- Parameters:
index- the index at which to insert.f- the float to insert.- Returns:
- this buffer.
- Throws:
StringIndexOutOfBoundsException- ifindex < 0orindex > length().
-
insert
Inserts the string representation of the specified object into this buffer at the specified offset.If the specified object is
null, the string"null"is inserted, otherwise the objectstoStringmethod is used to get its string representation.- Parameters:
index- the index at which to insert.obj- the object to insert (may be null).- Returns:
- this buffer.
- Throws:
StringIndexOutOfBoundsException- ifindex < 0orindex > length().
-
insert
Inserts the string into this buffer at the specified offset.If the specified string is
null, the string"null"is inserted, otherwise the contents of the string is inserted.- Parameters:
index- the index at which to insert.string- the string to insert (may be null).- Returns:
- this buffer.
- Throws:
StringIndexOutOfBoundsException- ifindex < 0orindex > length().
-
insert
Inserts the character array into this buffer at the specified offset.- Parameters:
index- the index at which to insert.chars- the character array to insert.- Returns:
- this buffer.
- Throws:
StringIndexOutOfBoundsException- ifindex < 0orindex > length().NullPointerException- ifcharsisnull.
-
insert
Inserts the specified subsequence of characters into this buffer at the specified index.- Parameters:
index- the index at which to insert.chars- the character array to insert.start- the starting offset.length- the number of characters.- Returns:
- this buffer.
- Throws:
NullPointerException- ifcharsisnull.StringIndexOutOfBoundsException- iflength < 0,start < 0,start + length > chars.length,index < 0orindex > length()
-
insert
Inserts the specified CharSequence into this buffer at the specified index.If the specified CharSequence is
null, the string"null"is inserted, otherwise the contents of the CharSequence.- Parameters:
index- The index at which to insert.s- The char sequence to insert.- Returns:
- this buffer.
- Throws:
IndexOutOfBoundsException- ifindex < 0orindex > length().- Since:
- 1.5
-
insert
Inserts the specified subsequence into this buffer at the specified index.If the specified CharSequence is
null, the string"null"is inserted, otherwise the contents of the CharSequence.- Parameters:
index- The index at which to insert.s- The char sequence to insert.start- The inclusive start index in the char sequence.end- The exclusive end index in the char sequence.- Returns:
- this buffer.
- Throws:
IndexOutOfBoundsException- ifindexis negative or greater than the current length,startorendare negative,startis greater thanendorendis greater than the length ofs.- Since:
- 1.5
-
lastIndexOf
Searches for the index of the specified character. The search for the character starts at the specified offset and moves towards the beginning.- Parameters:
subString- the string to find.start- the starting offset.- Returns:
- the index of the specified character, -1 if the character isn't found.
- See Also:
String.lastIndexOf(String,int)
-
offsetByCodePoints
public int offsetByCodePoints(int index, int codePointOffset)Returns the index that is offsetcodePointOffsetcode points fromindex.- Parameters:
index- the index to calculate the offset from.codePointOffset- the number of code points to count.- Returns:
- the index that is
codePointOffsetcode points away from index. - See Also:
Character,Character.offsetByCodePoints(char[], int, int, int, int)
-
replace
Replaces the characters in the specified range with the contents of the specified string.- Parameters:
start- the inclusive begin index.end- the exclusive end index.string- the string that will replace the contents in the range.- Returns:
- this buffer.
- Throws:
StringIndexOutOfBoundsException- ifstartorendare negative,startis greater thanendorendis greater than the length ofs.
-
reverse
Reverses the order of characters in this buffer.- Returns:
- this buffer.
-
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.
-
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
-