Serializable, Appendable, CharSequencepublic final class StringBuffer extends Object implements Appendable, CharSequence, Serializable
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.
Note that while StringBuffer is designed to be safe to use
concurrently from multiple threads, if the constructor or the
append or insert operation is passed a source sequence
that is shared across threads, the calling code must ensure
that the operation has a consistent and unchanging view of the source
sequence for the duration of the operation.
This could be satisfied by the caller holding a lock during the
operation's call, by using an immutable source sequence, or by not
sharing the source sequence across threads.
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.
Unless otherwise noted, passing a null argument to a constructor
or method in this class will cause a NullPointerException to be
thrown.
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.
StringBuilder,
String,
序列化表格| 构造器 | 说明 |
|---|---|
StringBuffer() |
Constructs a string buffer with no characters in it and an
initial capacity of 16 characters.
|
StringBuffer(int capacity) |
Constructs a string buffer with no characters in it and
the specified initial capacity.
|
StringBuffer(CharSequence seq) |
Constructs a string buffer that contains the same characters
as the specified
CharSequence. |
StringBuffer(String str) |
Constructs a string buffer initialized to the contents of the
specified string.
|
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
StringBuffer |
append(boolean b) |
|
StringBuffer |
append(char c) |
Appends the specified character to this Appendable.
|
StringBuffer |
append(char[] str) |
|
StringBuffer |
append(char[] str,
int offset,
int len) |
|
StringBuffer |
append(double d) |
|
StringBuffer |
append(float f) |
|
StringBuffer |
append(int i) |
|
StringBuffer |
append(long lng) |
|
StringBuffer |
append(CharSequence s) |
Appends the specified
CharSequence to this
sequence. |
StringBuffer |
append(CharSequence s,
int start,
int end) |
Appends a subsequence of the specified character sequence to this
Appendable.
|
StringBuffer |
append(Object obj) |
|
StringBuffer |
append(String str) |
|
StringBuffer |
append(StringBuffer sb) |
Appends the specified
StringBuffer to this sequence. |
StringBuffer |
appendCodePoint(int codePoint) |
|
int |
capacity() |
|
char |
charAt(int index) |
Returns the
char value at the specified index. |
int |
codePointAt(int index) |
|
int |
codePointBefore(int index) |
|
int |
codePointCount(int beginIndex,
int endIndex) |
|
StringBuffer |
delete(int start,
int end) |
|
StringBuffer |
deleteCharAt(int index) |
|
void |
ensureCapacity(int minimumCapacity) |
|
void |
getChars(int srcBegin,
int srcEnd,
char[] dst,
int dstBegin) |
|
int |
indexOf(String str) |
|
int |
indexOf(String str,
int fromIndex) |
|
StringBuffer |
insert(int offset,
boolean b) |
|
StringBuffer |
insert(int offset,
char c) |
|
StringBuffer |
insert(int offset,
char[] str) |
|
StringBuffer |
insert(int index,
char[] str,
int offset,
int len) |
|
StringBuffer |
insert(int offset,
double d) |
|
StringBuffer |
insert(int offset,
float f) |
|
StringBuffer |
insert(int offset,
int i) |
|
StringBuffer |
insert(int offset,
long l) |
|
StringBuffer |
insert(int dstOffset,
CharSequence s) |
|
StringBuffer |
insert(int dstOffset,
CharSequence s,
int start,
int end) |
|
StringBuffer |
insert(int offset,
Object obj) |
|
StringBuffer |
insert(int offset,
String str) |
|
int |
lastIndexOf(String str) |
|
int |
lastIndexOf(String str,
int fromIndex) |
|
int |
length() |
Returns the length of this character sequence.
|
int |
offsetByCodePoints(int index,
int codePointOffset) |
|
StringBuffer |
replace(int start,
int end,
String str) |
|
StringBuffer |
reverse() |
|
void |
setCharAt(int index,
char ch) |
|
void |
setLength(int newLength) |
|
CharSequence |
subSequence(int start,
int end) |
Returns a
CharSequence that is a subsequence of this sequence. |
String |
substring(int start) |
|
String |
substring(int start,
int end) |
|
String |
toString() |
Returns a string representation of the object.
|
void |
trimToSize() |
chars, codePointspublic StringBuffer()
public StringBuffer(int capacity)
capacity - the initial capacity.NegativeArraySizeException - if the capacity
argument is less than 0.public StringBuffer(String str)
16 plus the length of the string argument.str - the initial contents of the buffer.public StringBuffer(CharSequence seq)
CharSequence. The initial capacity of
the string buffer is 16 plus the length of the
CharSequence argument.
If the length of the specified CharSequence is
less than or equal to zero, then an empty buffer of capacity
16 is returned.
seq - the sequence to copy.public int length()
CharSequencechars in the sequence.length 在接口中 CharSequencechars in this sequencepublic int capacity()
public void ensureCapacity(int minimumCapacity)
public void trimToSize()
public void setLength(int newLength)
IndexOutOfBoundsExceptionlength()public char charAt(int index)
CharSequencechar value at the specified index. An index ranges from zero
to length() - 1. The first char value of the sequence is at
index zero, the next at index one, and so on, as for array
indexing.
If the char value specified by the index is a
surrogate, the surrogate
value is returned.
charAt 在接口中 CharSequenceindex - the index of the char value to be returnedchar valueIndexOutOfBoundsException - if the index argument is negative or not less than
length()length()public int codePointAt(int index)
public int codePointBefore(int index)
public int codePointCount(int beginIndex,
int endIndex)
public int offsetByCodePoints(int index,
int codePointOffset)
public void getChars(int srcBegin,
int srcEnd,
char[] dst,
int dstBegin)
public void setCharAt(int index,
char ch)
IndexOutOfBoundsExceptionlength()public StringBuffer append(Object obj)
public StringBuffer append(String str)
public StringBuffer append(StringBuffer sb)
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.public StringBuffer append(CharSequence s)
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.
append 在接口中 Appendables - the CharSequence to append.public StringBuffer append(CharSequence s, int start, int end)
AppendableAn invocation of this method of the form out.append(csq, start, end) when csq is not null, behaves in exactly the same way as the invocation
out.append(csq.subSequence(start, end)) append 在接口中 Appendables - The character sequence from which a subsequence will be
appended. If csq is null, then characters
will be appended as if csq contained the four
characters "null".start - The index of the first character in the subsequenceend - The index of the character following the last character in the
subsequenceIndexOutOfBoundsException - If start or end are negative, start
is greater than end, or end is greater than
csq.length()public StringBuffer append(char[] str)
public StringBuffer append(char[] str, int offset, int len)
public StringBuffer append(boolean b)
public StringBuffer append(char c)
Appendableappend 在接口中 Appendablec - The character to appendpublic StringBuffer append(int i)
public StringBuffer appendCodePoint(int codePoint)
public StringBuffer append(long lng)
public StringBuffer append(float f)
public StringBuffer append(double d)
public StringBuffer delete(int start, int end)
StringIndexOutOfBoundsExceptionpublic StringBuffer deleteCharAt(int index)
StringIndexOutOfBoundsExceptionpublic StringBuffer replace(int start, int end, String str)
StringIndexOutOfBoundsExceptionpublic String substring(int start)
StringIndexOutOfBoundsExceptionpublic CharSequence subSequence(int start, int end)
CharSequenceCharSequence that is a subsequence of this sequence.
The subsequence starts with the char value at the specified index and
ends with the char value at index end - 1. The length
(in chars) of the
returned sequence is end - start, so if start == end
then an empty sequence is returned.subSequence 在接口中 CharSequencestart - the start index, inclusiveend - the end index, exclusiveIndexOutOfBoundsException - if start or end are negative,
if end is greater than length(),
or if start is greater than endpublic String substring(int start, int end)
StringIndexOutOfBoundsExceptionpublic StringBuffer insert(int index, char[] str, int offset, int len)
StringIndexOutOfBoundsExceptionpublic 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)
IndexOutOfBoundsExceptionpublic StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
IndexOutOfBoundsExceptionpublic 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 indexOf(String str)
public int indexOf(String str, int fromIndex)
public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)
public StringBuffer reverse()
public String toString()
ObjecttoString method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
toString 在接口中 CharSequencetoString 在类中 Object