Extends:
AbstractStringBuilder
A thread-safe, mutable sequence of characters.
A string buffer is like a
String, but can be modified. At any
point in time it contains some particular sequence of characters, but
the length and content of the sequence can be changed through certain
method calls.
String buffers are safe for use by multiple threads. The methods
are synchronized where necessary so that all the operations on any
particular instance behave as if they occur in some serial order
that is consistent with the order of the method calls made by each of
the individual threads involved.
The principal operations on a StringBuffer are the
append and insert methods, which are
overloaded so as to accept data of any type. Each effectively
converts a given datum to a string and then appends or inserts the
characters of that string to the string buffer. The
append method always adds these characters at the end
of the buffer; the insert method adds the characters at
a specified point.
For example, if z refers to a string buffer object
whose current contents are "start", then
the method call z.append("le") would cause the string
buffer to contain "startle", whereas
z.insert(4, "le") would alter the string buffer to
contain "starlet".
In general, if sb refers to an instance of a StringBuffer,
then sb.append(x) has the same effect as
sb.insert(sb.length(), x).
Whenever an operation occurs involving a source sequence (such as
appending or inserting from a source sequence) this class synchronizes
only on the string buffer performing the operation, not on the source.
Every string buffer has a capacity. As long as the length of the
character sequence contained in the string buffer does not exceed
the capacity, it is not necessary to allocate a new internal
buffer array. If the internal buffer overflows, it is
automatically made larger.
As of release JDK 5, this class has been supplemented with an equivalent
class designed for use by a single thread, StringBuilder. The
StringBuilder class should generally be used in preference to
this one, as it supports all of the same operations but it is faster, as
it performs no synchronization.
| Methods |
| public StringBuffer |
append(Object obj)
|
| public StringBuffer |
append(String str)
|
| public StringBuffer |
append(StringBuffer sb) Appends the specified StringBuffer to this sequence.
The characters of the StringBuffer argument are appended,
in order, to the contents of this StringBuffer, increasing the
length of this StringBuffer by the length of the argument.
If sb is null, then the four characters
"null" are appended to this StringBuffer.
Let n be the length of the old character sequence, the one
contained in the StringBuffer just prior to execution of the
append method. Then the character at index k in
the new character sequence is equal to the character at index k
in the old character sequence, if k is less than n;
otherwise, it is equal to the character at index k-n in the
argument sb.
This method synchronizes on this (the destination)
object but does not synchronize on the source (sb).
| sb | the StringBuffer to append. |
| return | a reference to this object. |
| since | 1.4 |
|
| public StringBuffer |
append(CharSequence s) Appends the specified CharSequence to this
sequence.
The characters of the CharSequence argument are appended,
in order, increasing the length of this sequence by the length of the
argument.
The result of this method is exactly the same as if it were an
invocation of this.append(s, 0, s.length());
This method synchronizes on this (the destination)
object but does not synchronize on the source (s).
If s is null, then the four characters
"null" are appended.
| s | the CharSequence to append. |
| return | a reference to this object. |
| since | 1.5 |
|
| public StringBuffer |
append(CharSequence s, int start, int end)
| Throws | IndexOutOfBoundsException: if
start or end are negative, or
start is greater than end or
end is greater than s.length() |
| since | 1.5 |
|
| public StringBuffer |
append(char[] str)
|
| public StringBuffer |
append(char[] str, int offset, int len)
|
| public StringBuffer |
append(boolean b)
|
| public StringBuffer |
append(char c)
|
| public StringBuffer |
append(int i)
|
| public StringBuffer |
append(long lng)
|
| public StringBuffer |
append(float f)
|
| public StringBuffer |
append(double d)
|
| public StringBuffer |
appendCodePoint(int codePoint)
|
| public int |
capacity()
|
| public char |
charAt(int index)
|
| public int |
codePointAt(int index)
|
| public int |
codePointBefore(int index)
|
| public int |
codePointCount(int beginIndex, int endIndex)
|
| public StringBuffer |
delete(int start, int end)
|
| public StringBuffer |
deleteCharAt(int index)
|
| public void |
ensureCapacity(int minimumCapacity)
|
| public int |
indexOf(String str)
|
| public int |
indexOf(String str, int fromIndex)
|
| public StringBuffer |
insert(int index, char[] str, int offset, int len)
| Throws | StringIndexOutOfBoundsException: if index
is negative or greater than length(), or
offset or len are negative, or
(offset+len) is greater than
str.length. |
| since | 1.2 |
|
| public StringBuffer |
insert(int offset, Object obj)
|
| public StringBuffer |
insert(int offset, String str)
|
| public StringBuffer |
insert(int offset, char[] str)
|
| public StringBuffer |
insert(int dstOffset, CharSequence s)
|
| public StringBuffer |
insert(int dstOffset, CharSequence s, int start, int end)
| Throws | IndexOutOfBoundsException: if dstOffset
is negative or greater than this.length(), or
start or end are negative, or
start is greater than end or
end is greater than s.length() |
| since | 1.5 |
|
| public StringBuffer |
insert(int offset, boolean b)
|
| public StringBuffer |
insert(int offset, char c)
|
| public StringBuffer |
insert(int offset, int i)
|
| public StringBuffer |
insert(int offset, long l)
|
| public StringBuffer |
insert(int offset, float f)
|
| public StringBuffer |
insert(int offset, double d)
|
| public int |
lastIndexOf(String str)
|
| public int |
lastIndexOf(String str, int fromIndex)
|
| public int |
length()
|
| public int |
offsetByCodePoints(int index, int codePointOffset)
|
| public StringBuffer |
replace(int start, int end, String str)
|
| public StringBuffer |
reverse()
|
| public CharSequence |
subSequence(int start, int end)
| Throws | IndexOutOfBoundsException: if start or end are negative,
if end is greater than length(),
or if start is greater than end |
| since | 1.4 |
|
| public String |
substring(int start)
|
| public String |
substring(int start, int end)
|
| public String |
toString()
|
| public void |
trimToSize()
|