java.lang
public final class StringBuffer extends AbstractStringBuilder implements Serializable, CharSequence
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)
.
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 1.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.
Since: JDK1.0
Version: 1.89, 07/11/03
See Also: java.lang.StringBuilder String
Constructor Summary | |
---|---|
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(String str)
Constructs a string buffer initialized to the contents of the
specified string. | |
StringBuffer(CharSequence seq)
Constructs a string buffer that contains the same characters
as the specified CharSequence . |
Method Summary | |
---|---|
StringBuffer | append(Object obj) |
StringBuffer | append(String str) |
StringBuffer | append(StringBuffer sb)
Appends the specified StringBuffer to this sequence.
|
StringBuffer | append(CharSequence s)
Appends the specified CharSequence to this
sequence.
|
StringBuffer | append(CharSequence s, int srcOffset, int len)
Appends a subsequence of the specified CharSequence to this
sequence.
|
StringBuffer | append(char[] str) |
StringBuffer | append(char[] str, int offset, int len) |
StringBuffer | append(boolean b) |
StringBuffer | append(char c) |
StringBuffer | append(int i) |
StringBuffer | append(long lng) |
StringBuffer | append(float f) |
StringBuffer | append(double d) |
int | capacity() |
char | charAt(int index) |
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 index, char[] str, int offset, int len) |
StringBuffer | insert(int offset, Object obj) |
StringBuffer | insert(int offset, String str) |
StringBuffer | insert(int offset, char[] str) |
StringBuffer | insert(int dstOffset, CharSequence s) |
StringBuffer | insert(int dstOffset, CharSequence s, int srcOffset, int len) |
StringBuffer | insert(int offset, boolean b) |
StringBuffer | insert(int offset, char c) |
StringBuffer | insert(int offset, int i) |
StringBuffer | insert(int offset, long l) |
StringBuffer | insert(int offset, float f) |
StringBuffer | insert(int offset, double d) |
int | lastIndexOf(String str) |
int | lastIndexOf(String str, int fromIndex) |
int | length() |
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) |
String | substring(int start) |
String | substring(int start, int end) |
String | toString() |
void | trimToSize() |
Parameters: capacity the initial capacity.
Throws: NegativeArraySizeException if the capacity
argument is less than 0
.
16
plus the length of the string argument.
Parameters: str the initial contents of the buffer.
Throws: NullPointerException if str
is null
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.
Parameters: seq the sequence to copy.
Throws: NullPointerException if seq
is null
Since: 1.5
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
).
Parameters: sb the StringBuffer to append.
Returns: a reference to this object.
Since: 1.4
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.
Parameters: s the CharSequence
to append.
Returns: a reference to this object.
Since: 1.5
CharSequence
to this
sequence.
Characters of the CharSequence
argument, starting at
index srcOffset
, are appended, in order, to the contents of
this sequence. The length of this sequence is increased by the value of
len
.
Let n be the length of this character sequence just prior to
execution of the append
method. Then the character at
index k in this character sequence becomes equal to the
character at index k in s, if k is less than n;
otherwise, it is equal to the character at index k+srcOffset-n
in the argument s
.
This method synchronizes on this (the destination) object but does not
synchronize on the source (s
).
If s
is null
, then this method appends
characters as if the s parameter was a sequence containing the four
characters "null"
.
Since: 1.5
See Also: length
Since: 1.2
Since: 1.2
Since: 1.4
Since: 1.4
Since: 1.2
See Also: valueOf StringBuffer length
See Also: length
Since: 1.5
Since: 1.5
See Also: String StringBuffer length
See Also: length
See Also: String StringBuffer length
See Also: String StringBuffer length
See Also: String StringBuffer length
See Also: String StringBuffer length
Since: 1.4
Since: 1.4
Since: 1.2
Since: JDK1.0.2
See Also: length
See Also: length
Since: 1.4
Since: 1.2
Since: 1.2
Since: 1.5