↳
java.lang.StringBuffer
Class Overview
A modifiable
For particularly complex string-building needs, consider
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().
Summary
Public Constructors
Constructs a new StringBuffer using the default capacity which is 16.
Constructs a new StringBuffer using the specified capacity.
Constructs a new StringBuffer containing the characters in the specified
string.
Constructs a StringBuffer and initializes it with the content from the
specified CharSequence.
Public Methods
synchronized
StringBuffer
Adds the specified sequence of characters to the end of this buffer.
Adds the string representation of the specified double to the end of this
StringBuffer.
Adds the string representation of the specified boolean to the end of
this StringBuffer.
Adds the string representation of the specified long to the end of this
StringBuffer.
Adds the string representation of the specified float to the end of this
StringBuffer.
synchronized
StringBuffer
Adds the character array to the end of this buffer.
Adds the string representation of the specified integer to the end of
this StringBuffer.
synchronized
StringBuffer
Adds the specified StringBuffer to the end of this buffer.
synchronized
StringBuffer
Appends the specified CharSequence to this buffer.
synchronized
StringBuffer
Adds the specified character to the end of this buffer.
synchronized
StringBuffer
Adds the specified string to the end of this buffer.
synchronized
StringBuffer
CharSequence s, int start, int end)
Appends the specified subsequence of the CharSequence to this buffer.
synchronized
StringBuffer
Adds the string representation of the specified object to the end of this
StringBuffer.
Appends the string representation of the specified Unicode code point to
the end of this buffer.
int
Returns the number of characters that can be held without growing.
synchronized
char
Retrieves the character at the index.
synchronized
int
Retrieves the Unicode code point value at the index.
synchronized
int
Retrieves the Unicode code point value that precedes the index.
synchronized
int
Calculates the number of Unicode code points between start
and end.
synchronized
StringBuffer
Deletes a range of characters.
synchronized
StringBuffer
Deletes the character at the specified offset.
synchronized
void
Ensures that this object has a minimum capacity available before
requiring the internal buffer to be enlarged.
synchronized
void
Copies the requested sequence of characters to the char[] passed
starting at idx.
synchronized
int
String subString, int start)
Searches for the index of the specified character.
int
Searches for the first index of the specified character.
synchronized
StringBuffer
CharSequence s, int start, int end)
Inserts the specified subsequence into this buffer at the specified
index.
Inserts the string representation of the specified integer into this
buffer at the specified offset.
Inserts the string representation of the specified into this buffer
double at the specified offset.
synchronized
StringBuffer
Inserts the specified subsequence of characters into this buffer at the
specified index.
synchronized
StringBuffer
String string)
Inserts the string into this buffer at the specified offset.
Inserts the string representation of the specified long into this buffer
at the specified offset.
Object obj)
Inserts the string representation of the specified object into this
buffer at the specified offset.
Inserts the string representation of the specified float into this buffer
at the specified offset.
synchronized
StringBuffer
Inserts the character into this buffer at the specified offset.
synchronized
StringBuffer
Inserts the character array into this buffer at the specified offset.
synchronized
StringBuffer
Inserts the specified CharSequence into this buffer at the specified
index.
Inserts the string representation of the specified boolean into this
buffer at the specified offset.
int
Searches for the last index of the specified character.
synchronized
int
String subString, int start)
Searches for the index of the specified character.
int
The current length.
synchronized
int
Returns the index that is offset codePointOffset code points from
index.
synchronized
StringBuffer
String string)
Replaces the characters in the specified range with the contents of the
specified string.
synchronized
StringBuffer
Reverses the order of characters in this buffer.
synchronized
void
Sets the character at the index.
synchronized
void
Sets the current length to a new value.
synchronized
CharSequence
Returns a CharSequence of the subsequence from the start
index to the end index.
synchronized
String
Returns the String value of the subsequence from the start index
to the current end.
synchronized
String
Returns the String value of the subsequence from the start index
to the end index.
synchronized
String
Returns the current String representation.
synchronized
void
Trims off any extra capacity beyond the current length.
[Expand]
Inherited Methods
From class
java.lang.Object
Creates and returns a copy of this Object.
boolean
Compares this instance with the specified object and indicates if they
are equal.
void
Invoked when the garbage collector has detected that this instance is no longer reachable.
final
Class>
Returns the unique instance of
int
Returns an integer hash code for this object.
final
void
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
final
void
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
Returns a string containing a concise, human-readable description of this
object.
final
void
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
final
void
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
final
void
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
From interface
java.lang.Appendable
Appends the character sequence csq.
CharSequence csq, int start, int end)
Appends a subsequence of csq.
Appends the specified character.
From interface
java.lang.CharSequence
abstract
char
Returns the character at the specified index, with the first character
having index zero.
abstract
int
Returns the number of characters in this sequence.
Returns a CharSequence from the start index (inclusive)
to the end index (exclusive) of this sequence.
abstract
String
Returns a string with the same characters in the same order as in this
sequence.
Public Constructors
public
StringBuffer
()
Constructs a new StringBuffer using the default capacity which is 16.
public
StringBuffer
(int capacity)
Constructs a new StringBuffer using the specified capacity.
Parameters
capacitythe initial capacity.
public
StringBuffer
(String string)
Constructs a new StringBuffer containing the characters in the specified
string. The capacity of the new buffer will be the length of the
String plus the default capacity.
Parameters
stringthe string content with which to initialize the new instance.
Throws
public
StringBuffer
Constructs a StringBuffer and initializes it with the content from the
specified CharSequence. The capacity of the new buffer will be
the length of the CharSequence plus the default capacity.
Parameters
csthe content to initialize the instance.
Public Methods
public
synchronized
StringBuffer
append
(char[] chars, int start, int length)
Adds the specified sequence of characters to the end of this buffer.
Parameters
charsthe character array to append.
startthe starting offset.
lengththe number of characters.
Returns
this StringBuffer.
Throws
if length < 0 , start < 0 or start +
length > chars.length.
append
(double d)
Adds the string representation of the specified double to the end of this
StringBuffer.
Parameters
dthe double to append.
Returns
this StringBuffer.
append
(boolean b)
Adds the string representation of the specified boolean to the end of
this StringBuffer.
If the argument is true the string "true" is appended,
otherwise the string "false" is appended.
Parameters
bthe boolean to append.
Returns
this StringBuffer.
append
(long l)
Adds the string representation of the specified long to the end of this
StringBuffer.
Parameters
lthe long to append.
Returns
this StringBuffer.
append
(float f)
Adds the string representation of the specified float to the end of this
StringBuffer.
Parameters
fthe float to append.
Returns
this StringBuffer.
public
synchronized
StringBuffer
append
(char[] chars)
Adds the character array to the end of this buffer.
Parameters
charsthe character array to append.
Returns
this StringBuffer.
Throws
append
(int i)
Adds the string representation of the specified integer to the end of
this StringBuffer.
Parameters
ithe integer to append.
Returns
this StringBuffer.
public
synchronized
StringBuffer
append
Adds the specified StringBuffer to the end of this buffer.
If the specified StringBuffer is null the string "null"
is appended, otherwise the contents of the specified StringBuffer is
appended.
Parameters
sbthe StringBuffer to append (may be null).
Returns
this StringBuffer.
public
synchronized
StringBuffer
append
Appends the specified CharSequence to this buffer.
If the specified CharSequence is null the string "null"
is appended, otherwise the contents of the specified CharSequence is
appended.
Parameters
sthe CharSequence to append.
Returns
this StringBuffer.
public
synchronized
StringBuffer
append
(char ch)
Adds the specified character to the end of this buffer.
Parameters
chthe character to append.
Returns
this StringBuffer.
public
synchronized
StringBuffer
append
(String string)
Adds the specified string to the end of this buffer.
If the specified string is null the string "null" is
appended, otherwise the contents of the specified string is appended.
Parameters
stringthe string to append (may be null).
Returns
this StringBuffer.
public
synchronized
StringBuffer
append
(CharSequence s, int start, int end)
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.
Parameters
sthe CharSequence to append.
startthe inclusive start index.
endthe exclusive end index.
Returns
this StringBuffer.
Throws
if start or end are negative, start
is greater than end or end is greater than
the length of s.
public
synchronized
StringBuffer
append
Adds the string representation of the specified object to the end of this
StringBuffer.
If the specified object is null the string "null" is
appended, otherwise the objects toString is used to get its
string representation.
Parameters
objthe object to append (may be null).
Returns
this StringBuffer.
appendCodePoint
(int codePoint)
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 by
Parameters
codePointthe Unicode code point to encode and append.
Returns
this StringBuffer.
public
int
capacity
()
Returns the number of characters that can be held without growing.
Returns
the capacity
public
synchronized
char
charAt
(int index)
Retrieves the character at the index.
Parameters
indexthe index of the character to retrieve.
Returns
the char value.
public
synchronized
int
codePointAt
(int index)
Retrieves the Unicode code point value at the index.
Parameters
indexthe index to the char code unit.
Returns
the Unicode code point value.
public
synchronized
int
codePointBefore
(int index)
Retrieves the Unicode code point value that precedes the index.
Parameters
indexthe index to the char code unit within this object.
Returns
the Unicode code point value.
public
synchronized
int
codePointCount
(int beginIndex, int endIndex)
Calculates the number of Unicode code points between start
and end.
Parameters
beginIndexthe inclusive beginning index of the subsequence.
endIndexthe exclusive end index of the subsequence.
Returns
the number of Unicode code points in the subsequence.
public
synchronized
StringBuffer
delete
(int start, int end)
Deletes a range of characters.
Parameters
startthe offset of the first character.
endthe offset one past the last character.
Returns
this StringBuffer.
Throws
if start < 0, start > end or end >
length().
public
synchronized
StringBuffer
deleteCharAt
(int location)
Deletes the character at the specified offset.
Parameters
locationthe offset of the character to delete.
Returns
this StringBuffer.
Throws
if location < 0 or location >= length()
public
synchronized
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 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.
Parameters
minthe new minimum capacity to set.
public
synchronized
void
getChars
(int start, int end, char[] buffer, int idx)
Copies the requested sequence of characters to the char[] passed
starting at idx.
Parameters
startthe starting offset of characters to copy.
endthe ending offset of characters to copy.
bufferthe destination character array.
idxthe starting offset in the character array.
Throws
if start < 0, end > length(), start >
end, index < 0, end - start > buffer.length -
index
public
synchronized
int
indexOf
(String subString, int start)
Searches for the index of the specified character. The search for the
character starts at the specified offset and moves towards the end.
Parameters
subStringthe string to find.
startthe starting offset.
Returns
the index of the specified character, -1 if the character isn't
found
public
int
indexOf
(String string)
Searches for the first index of the specified character. The search for
the character starts at the beginning and moves towards the end.
Parameters
stringthe string to find.
Returns
the index of the specified character, -1 if the character isn't
found.
public
synchronized
StringBuffer
insert
(int index, CharSequence s, int start, int end)
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
indexThe index at which to insert.
sThe char sequence to insert.
startThe inclusive start index in the char sequence.
endThe exclusive end index in the char sequence.
Returns
this buffer.
Throws
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.
insert
(int index, int i)
Inserts the string representation of the specified integer into this
buffer at the specified offset.
Parameters
indexthe index at which to insert.
ithe integer to insert.
Returns
this buffer.
Throws
insert
(int index, double d)
Inserts the string representation of the specified into this buffer
double at the specified offset.
Parameters
indexthe index at which to insert.
dthe double to insert.
Returns
this buffer.
Throws
public
synchronized
StringBuffer
insert
(int index, char[] chars, int start, int length)
Inserts the specified subsequence of characters into this buffer at the
specified index.
Parameters
indexthe index at which to insert.
charsthe character array to insert.
startthe starting offset.
lengththe number of characters.
Returns
this buffer.
Throws
if length < 0, start < 0, start +
length > chars.length, index < 0 or index >
length()
public
synchronized
StringBuffer
insert
(int index, String string)
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
indexthe index at which to insert.
stringthe string to insert (may be null).
Returns
this buffer.
Throws
insert
(int index, long l)
Inserts the string representation of the specified long into this buffer
at the specified offset.
Parameters
indexthe index at which to insert.
lthe long to insert.
Returns
this buffer.
Throws
insert
(int index, Object obj)
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 objects toString method is used to get
its string representation.
Parameters
indexthe index at which to insert.
objthe object to insert (may be null).
Returns
this buffer.
Throws
insert
(int index, float f)
Inserts the string representation of the specified float into this buffer
at the specified offset.
Parameters
indexthe index at which to insert.
fthe float to insert.
Returns
this buffer.
Throws
public
synchronized
StringBuffer
insert
(int index, char ch)
Inserts the character into this buffer at the specified offset.
Parameters
indexthe index at which to insert.
chthe character to insert.
Returns
this buffer.
Throws
if index < 0 or index > length().
public
synchronized
StringBuffer
insert
(int index, char[] chars)
Inserts the character array into this buffer at the specified offset.
Parameters
indexthe index at which to insert.
charsthe character array to insert.
Returns
this buffer.
public
synchronized
StringBuffer
insert
(int index, CharSequence s)
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
indexThe index at which to insert.
sThe char sequence to insert.
Returns
this buffer.
Throws
if index < 0 or index > length().
insert
(int index, boolean b)
Inserts the string representation of the specified boolean into this
buffer at the specified offset.
Parameters
indexthe index at which to insert.
bthe boolean to insert.
Returns
this buffer.
Throws
public
int
lastIndexOf
(String string)
Searches for the last index of the specified character. The search for
the character starts at the end and moves towards the beginning.
Parameters
stringthe string to find.
Returns
the index of the specified character, -1 if the character isn't
found.
Throws
public
synchronized
int
lastIndexOf
(String subString, int start)
Searches for the index of the specified character. The search for the
character starts at the specified offset and moves towards the beginning.
Parameters
subStringthe string to find.
startthe starting offset.
Returns
the index of the specified character, -1 if the character isn't
found.
public
int
length
()
The current length.
Returns
the number of characters contained in this instance.
public
synchronized
int
offsetByCodePoints
(int index, int codePointOffset)
Returns the index that is offset codePointOffset code points from
index.
Parameters
indexthe index to calculate the offset from.
codePointOffsetthe number of code points to count.
Returns
the index that is codePointOffset code points away from
index.
public
synchronized
StringBuffer
replace
(int start, int end, String string)
Replaces the characters in the specified range with the contents of the
specified string.
Parameters
startthe inclusive begin index.
endthe exclusive end index.
stringthe string that will replace the contents in the range.
Returns
this buffer.
Throws
if start or end are negative, start
is greater than end or end is greater than
the length of s.
public
synchronized
StringBuffer
reverse
()
Reverses the order of characters in this buffer.
Returns
this buffer.
public
synchronized
void
setCharAt
(int index, char ch)
Sets the character at the index.
Parameters
indexthe zero-based index of the character to replace.
chthe character to set.
public
synchronized
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 the char value of
Returns the String value of the subsequence from the start index
to the current end.
Parameters
startthe inclusive start index to begin the subsequence.
Returns
a String containing the subsequence.
public
synchronized
String
substring
(int start, int end)
Returns the String value of the subsequence from the start index
to the end index.
Parameters
startthe inclusive start index to begin the subsequence.
endthe exclusive end index to end the subsequence.
Returns
a String containing the subsequence.
public
synchronized
String
toString
()
Returns the current String representation.
Returns
a String containing the characters in this instance.
public
synchronized
void
trimToSize
()
Trims off any extra capacity beyond the current length. Note, this method
is NOT guaranteed to change the capacity of this object.
Except as noted, this content is licensed under Apache 2.0.
For details and restrictions, see the Content License.
Android 4.2 r1 —