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()
.
CharSequence
,
Appendable
,
StringBuilder
,
String
,
String.format(java.lang.String, java.lang.Object...)
,
Serialized FormConstructor and Description |
---|
StringBuffer()
Constructs a new StringBuffer using the default capacity which is 16.
|
StringBuffer(CharSequence cs)
Constructs a StringBuffer and initializes it with the content from the
specified
CharSequence . |
StringBuffer(int capacity)
Constructs a new StringBuffer using the specified capacity.
|
StringBuffer(String string)
Constructs a new StringBuffer containing the characters in the specified
string.
|
Modifier and Type | Method and Description |
---|---|
StringBuffer |
append(boolean b)
Adds the string representation of the specified boolean to the end of
this StringBuffer.
|
StringBuffer |
append(char ch)
Adds the specified character to the end of this buffer.
|
StringBuffer |
append(char[] chars)
Adds the character array to the end of this buffer.
|
StringBuffer |
append(char[] chars,
int start,
int length)
Adds the specified sequence of characters to the end of this buffer.
|
StringBuffer |
append(CharSequence s)
Appends the specified CharSequence to this buffer.
|
StringBuffer |
append(CharSequence s,
int start,
int end)
Appends the specified subsequence of the CharSequence to this buffer.
|
StringBuffer |
append(double d)
Adds the string representation of the specified double to the end of this
StringBuffer.
|
StringBuffer |
append(float f)
Adds the string representation of the specified float to the end of this
StringBuffer.
|
StringBuffer |
append(int i)
Adds the string representation of the specified integer to the end of
this StringBuffer.
|
StringBuffer |
append(long l)
Adds the string representation of the specified long to the end of this
StringBuffer.
|
StringBuffer |
append(Object obj)
Adds the string representation of the specified object to the end of this
StringBuffer.
|
StringBuffer |
append(String string)
Adds the specified string to the end of this buffer.
|
StringBuffer |
append(StringBuffer sb)
Adds the specified StringBuffer to the end of this buffer.
|
StringBuffer |
appendCodePoint(int codePoint)
Appends the string representation of the specified Unicode code point to
the end of this buffer.
|
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 beginIndex,
int endIndex)
Calculates the number of Unicode code points between
start
and end . |
StringBuffer |
delete(int start,
int end)
Deletes a range of characters.
|
StringBuffer |
deleteCharAt(int location)
Deletes the character at the specified offset.
|
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[] buffer,
int idx)
Copies the requested sequence of characters to the
char[] passed
starting at idx . |
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.
|
StringBuffer |
insert(int index,
boolean b)
Inserts the string representation of the specified boolean into this
buffer at the specified offset.
|
StringBuffer |
insert(int index,
char ch)
Inserts the character into this buffer at the specified offset.
|
StringBuffer |
insert(int index,
char[] chars)
Inserts the character array into this buffer at the specified offset.
|
StringBuffer |
insert(int index,
char[] chars,
int start,
int length)
Inserts the specified subsequence of characters into this buffer at the
specified index.
|
StringBuffer |
insert(int index,
CharSequence s)
Inserts the specified CharSequence into this buffer at the specified
index.
|
StringBuffer |
insert(int index,
CharSequence s,
int start,
int end)
Inserts the specified subsequence into this buffer at the specified
index.
|
StringBuffer |
insert(int index,
double d)
Inserts the string representation of the specified into this buffer
double at the specified offset.
|
StringBuffer |
insert(int index,
float f)
Inserts the string representation of the specified float into this buffer
at the specified offset.
|
StringBuffer |
insert(int index,
int i)
Inserts the string representation of the specified integer into this
buffer at the specified offset.
|
StringBuffer |
insert(int index,
long l)
Inserts the string representation of the specified long into this buffer
at the specified offset.
|
StringBuffer |
insert(int index,
Object obj)
Inserts the string representation of the specified object into this
buffer at the specified offset.
|
StringBuffer |
insert(int index,
String string)
Inserts the string into this buffer 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 . |
StringBuffer |
replace(int start,
int end,
String string)
Replaces the characters in the specified range with the contents of the
specified string.
|
StringBuffer |
reverse()
Reverses the order of characters in this buffer.
|
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 current String representation.
|
void |
trimToSize()
Trims off any extra capacity beyond the current length.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
length
public StringBuffer()
public StringBuffer(int capacity)
capacity
- the initial capacity.public StringBuffer(String string)
String
plus the default capacity.string
- the string content with which to initialize the new instance.NullPointerException
- if string
is null
.public StringBuffer(CharSequence cs)
CharSequence
. The capacity of the new buffer will be
the length of the CharSequence
plus the default capacity.cs
- the content to initialize the instance.NullPointerException
- if cs
is null
.public StringBuffer append(boolean b)
If the argument is true
the string "true"
is appended,
otherwise the string "false"
is appended.
b
- the boolean to append.String.valueOf(boolean)
public StringBuffer append(char ch)
append
in interface Appendable
ch
- the character to append.String.valueOf(char)
public StringBuffer append(double d)
d
- the double to append.String.valueOf(double)
public StringBuffer append(float f)
f
- the float to append.String.valueOf(float)
public StringBuffer append(int i)
i
- the integer to append.String.valueOf(int)
public StringBuffer append(long l)
l
- the long to append.String.valueOf(long)
public StringBuffer append(Object obj)
If the specified object is null
the string "null"
is
appended, otherwise the objects toString
is used to get its
string representation.
obj
- the object to append (may be null).String.valueOf(Object)
public StringBuffer append(String string)
If the specified string is null
the string "null"
is
appended, otherwise the contents of the specified string is appended.
string
- the string to append (may be null).public StringBuffer append(StringBuffer sb)
If the specified StringBuffer is null
the string "null"
is appended, otherwise the contents of the specified StringBuffer is
appended.
sb
- the StringBuffer to append (may be null).public StringBuffer append(char[] chars)
chars
- the character array to append.NullPointerException
- if chars
is null
.public StringBuffer append(char[] chars, int start, int length)
chars
- the character array to append.start
- the starting offset.length
- the number of characters.ArrayIndexOutOfBoundsException
- if length < 0
, start < 0
or start +
length > chars.length
.NullPointerException
- if chars
is null
.public StringBuffer append(CharSequence s)
If the specified CharSequence is null
the string "null"
is appended, otherwise the contents of the specified CharSequence is
appended.
append
in interface Appendable
s
- the CharSequence to append.public StringBuffer append(CharSequence s, int start, int end)
If the specified CharSequence is null
, then the string "null"
is used to extract a subsequence.
append
in interface Appendable
s
- the CharSequence to append.start
- the inclusive start index.end
- the exclusive end index.IndexOutOfBoundsException
- if start
or end
are negative, start
is greater than end
or end
is greater than
the length of s
.public StringBuffer appendCodePoint(int codePoint)
The code point is converted to a char[]
as defined by
Character.toChars(int)
.
codePoint
- the Unicode code point to encode and append.Character.toChars(int)
public char charAt(int index)
index
.charAt
in interface CharSequence
index
- the index of the character to retrieve.public int codePointAt(int index)
index
.index
- the index to the char
code unit.Character
,
Character.codePointAt(char[], int, int)
public int codePointBefore(int index)
index
.index
- the index to the char
code unit within this object.Character
,
Character.codePointBefore(char[], int, int)
public int codePointCount(int beginIndex, int endIndex)
start
and end
.beginIndex
- the inclusive beginning index of the subsequence.endIndex
- the exclusive end index of the subsequence.Character
,
Character.codePointCount(char[], int, int)
public StringBuffer delete(int start, int end)
start
- the offset of the first character.end
- the offset one past the last character.StringIndexOutOfBoundsException
- if start < 0
, start > end
or end >
length()
.public StringBuffer deleteCharAt(int location)
location
- the offset of the character to delete.StringIndexOutOfBoundsException
- if location < 0
or location >= 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[] buffer, int idx)
char[]
passed
starting at idx
.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.IndexOutOfBoundsException
- if start < 0
, end > length()
, start >
end
, index < 0
, end - start > buffer.length -
index
public int indexOf(String subString, int start)
subString
- the string to find.start
- the starting offset.lastIndexOf(String,int)
public StringBuffer insert(int index, char ch)
index
- the index at which to insert.ch
- the character to insert.ArrayIndexOutOfBoundsException
- if index < 0
or index > length()
.public StringBuffer insert(int index, boolean b)
index
- the index at which to insert.b
- the boolean to insert.StringIndexOutOfBoundsException
- if index < 0
or index > length()
.public StringBuffer insert(int index, int i)
index
- the index at which to insert.i
- the integer to insert.StringIndexOutOfBoundsException
- if index < 0
or index > length()
.public StringBuffer insert(int index, long l)
index
- the index at which to insert.l
- the long to insert.StringIndexOutOfBoundsException
- if index < 0
or index > length()
.public StringBuffer insert(int index, double d)
index
- the index at which to insert.d
- the double to insert.StringIndexOutOfBoundsException
- if index < 0
or index > length()
.public StringBuffer insert(int index, float f)
index
- the index at which to insert.f
- the float to insert.StringIndexOutOfBoundsException
- if index < 0
or index > length()
.public StringBuffer insert(int index, Object obj)
If the specified object is null
, the string "null"
is
inserted, otherwise the objects toString
method is used to get
its string representation.
index
- the index at which to insert.obj
- the object to insert (may be null).StringIndexOutOfBoundsException
- if index < 0
or index > length()
.public StringBuffer insert(int index, String string)
If the specified string is null
, the string "null"
is
inserted, otherwise the contents of the string is inserted.
index
- the index at which to insert.string
- the string to insert (may be null).StringIndexOutOfBoundsException
- if index < 0
or index > length()
.public StringBuffer insert(int index, char[] chars)
index
- the index at which to insert.chars
- the character array to insert.StringIndexOutOfBoundsException
- if index < 0
or index > length()
.NullPointerException
- if chars
is null
.public StringBuffer insert(int index, char[] chars, int start, int length)
index
- the index at which to insert.chars
- the character array to insert.start
- the starting offset.length
- the number of characters.NullPointerException
- if chars
is null
.StringIndexOutOfBoundsException
- if length < 0
, start < 0
, start +
length > chars.length
, index < 0
or index >
length()
public StringBuffer insert(int index, CharSequence s)
If the specified CharSequence is null
, the string "null"
is inserted, otherwise the contents of the CharSequence.
index
- The index at which to insert.s
- The char sequence to insert.IndexOutOfBoundsException
- if index < 0
or index > length()
.public StringBuffer insert(int index, CharSequence s, int start, int end)
If the specified CharSequence is null
, the string "null"
is inserted, otherwise the contents of the CharSequence.
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.IndexOutOfBoundsException
- if index
is negative or greater than the current
length, start
or end
are negative, start
is greater than end
or end
is greater
than the length of s
.public int lastIndexOf(String subString, int start)
subString
- the string to find.start
- the starting offset.String.lastIndexOf(String,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.Character
,
Character.offsetByCodePoints(char[], int, int, int, int)
public StringBuffer replace(int start, int end, String string)
start
- the inclusive begin index.end
- the exclusive end index.string
- the string that will replace the contents in the range.StringIndexOutOfBoundsException
- if start
or end
are negative, start
is greater than end
or end
is greater than
the length of s
.public StringBuffer reverse()
public void setCharAt(int index, char ch)
index
.index
- the zero-based index of the character to replace.ch
- the character to set.public void setLength(int length)
char
value of
.length
- the new length of this StringBuffer.length()
public CharSequence subSequence(int start, int end)
CharSequence
of the subsequence from the start
index to the end
index.subSequence
in interface CharSequence
start
- the inclusive start index to begin the subsequence.end
- the exclusive end index to end the subsequence.public String substring(int start)
start
index
to the current end.start
- the inclusive start index to begin the subsequence.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.public String toString()
toString
in interface CharSequence
public void trimToSize()
public int capacity()
ensureCapacity(int)
,
length()
public int length()
public int indexOf(String string)
string
- the string to find.lastIndexOf(String)
public int lastIndexOf(String string)
string
- the string to find.NullPointerException
- if string
is null
.String.lastIndexOf(java.lang.String)