StringBuffer
public final class StringBuffer
extends Object
implements Appendable, CharSequence, Comparable<StringBuffer>, Serializable
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. 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.
Summary
Public constructors |
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. |
Public methods |
StringBuffer | append(boolean b) |
StringBuffer | append(long lng) |
StringBuffer | append(char c) Appends the specified character to this Appendable . |
StringBuffer | append(Object obj) |
StringBuffer | append(char[] str, int offset, int len) |
StringBuffer | append(double d) |
StringBuffer | append(char[] str) |
StringBuffer | append(String str) |
StringBuffer | append(StringBuffer sb) Appends the specified StringBuffer to this sequence. |
StringBuffer | append(float f) |
StringBuffer | append(int i) |
StringBuffer | append(CharSequence s, int start, int end) Appends a subsequence of the specified character sequence to this Appendable . |
StringBuffer | append(CharSequence s) Appends the specified CharSequence to this sequence. |
StringBuffer | appendCodePoint(int codePoint) |
int | capacity() |
char | charAt(int index) Returns the char value at the specified index. |
IntStream | chars() Returns a stream of int zero-extending the char values from this sequence. |
int | codePointAt(int index) |
int | codePointBefore(int index) |
int | codePointCount(int beginIndex, int endIndex) |
IntStream | codePoints() Returns a stream of code point values from this sequence. |
int | compareTo(StringBuffer another) Compares two StringBuffer instances lexicographically. |
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, char[] str) |
StringBuffer | insert(int offset, float f) |
StringBuffer | insert(int dstOffset, CharSequence s) |
StringBuffer | insert(int offset, char c) |
StringBuffer | insert(int offset, long l) |
StringBuffer | insert(int index, char[] str, int offset, int len) |
StringBuffer | insert(int offset, int i) |
StringBuffer | insert(int offset, String str) |
StringBuffer | insert(int offset, double d) |
StringBuffer | insert(int dstOffset, CharSequence s, int start, int end) |
StringBuffer | insert(int offset, boolean b) |
StringBuffer | insert(int offset, Object obj) |
int | lastIndexOf(String str, int fromIndex) |
int | lastIndexOf(String str) |
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, int end) |
String | substring(int start) |
String | toString() Returns a string representation of the object. |
void | trimToSize() |
Inherited methods |
From class java.lang.Object Object | clone() Creates and returns a copy of this object. | boolean | equals(Object obj) Indicates whether some other object is "equal to" this one. | void | finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | final Class<?> | getClass() Returns the runtime class of this Object . | int | hashCode() Returns a hash code value for the object. | final void | notify() Wakes up a single thread that is waiting on this object's monitor. | final void | notifyAll() Wakes up all threads that are waiting on this object's monitor. | String | toString() Returns a string representation of the object. | final void | wait(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. | final void | wait(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. | final void | wait() Causes the current thread to wait until it is awakened, typically by being notified or interrupted. | |
|
From interface java.lang.CharSequence abstract char | charAt(int index) Returns the char value at the specified index. | default IntStream | chars() Returns a stream of int zero-extending the char values from this sequence. | default IntStream | codePoints() Returns a stream of code point values from this sequence. | static int | compare(CharSequence cs1, CharSequence cs2) Compares two CharSequence instances lexicographically. | default boolean | isEmpty() Returns true if this character sequence is empty. | abstract int | length() Returns the length of this character sequence. | abstract CharSequence | subSequence(int start, int end) Returns a CharSequence that is a subsequence of this sequence. | abstract String | toString() Returns a string containing the characters in this sequence in the same order as this sequence. | |
|
Public constructors
StringBuffer
public StringBuffer ()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
StringBuffer
public StringBuffer (int capacity)
Constructs a string buffer with no characters in it and the specified initial capacity.
Parameters |
capacity | int : the initial capacity. |
StringBuffer
public StringBuffer (CharSequence seq)
Constructs a string buffer that contains the same characters as the specified 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 | CharSequence : the sequence to copy. |
StringBuffer
public StringBuffer (String str)
Constructs a string buffer initialized to the contents of the specified string. The initial capacity of the string buffer is 16
plus the length of the string argument.
Parameters |
str | String : the initial contents of the buffer. |
Public methods
append
public StringBuffer append (char c)
Appends the specified character to this Appendable
.
Parameters |
c | char : The character to append |
append
public StringBuffer append (char[] str, int offset, int len)
Parameters |
str | char |
offset | int |
len | int |
append
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
).
Parameters |
sb | StringBuffer : the StringBuffer to append. |
append
public StringBuffer append (CharSequence s, int start, int end)
Appends a subsequence of the specified character sequence to this Appendable
.
An 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))
Parameters |
s | CharSequence : 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 | int : The index of the first character in the subsequence |
end | int : The index of the character following the last character in the subsequence |
append
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.
Parameters |
s | CharSequence : the CharSequence to append. |
appendCodePoint
public StringBuffer appendCodePoint (int codePoint)
capacity
public int capacity ()
charAt
public char charAt (int index)
Returns the char
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.
Parameters |
index | int : the index of the char value to be returned |
Returns |
char | the specified char value |
chars
public IntStream chars ()
Returns a stream of int
zero-extending the char
values from this sequence. Any char which maps to a surrogate code point is passed through uninterpreted.
The stream binds to this sequence when the terminal stream operation commences (specifically, for mutable sequences the spliterator for the stream is late-binding). If the sequence is modified during that operation then the result is undefined.
Returns |
IntStream | an IntStream of char values from this sequence |
codePointAt
public int codePointAt (int index)
codePointBefore
public int codePointBefore (int index)
codePointCount
public int codePointCount (int beginIndex, int endIndex)
Parameters |
beginIndex | int |
endIndex | int |
codePoints
public IntStream codePoints ()
Returns a stream of code point values from this sequence. Any surrogate pairs encountered in the sequence are combined as if by Character.toCodePoint and the result is passed to the stream. Any other code units, including ordinary BMP characters, unpaired surrogates, and undefined code units, are zero-extended to int
values which are then passed to the stream.
The stream binds to this sequence when the terminal stream operation commences (specifically, for mutable sequences the spliterator for the stream is late-binding). If the sequence is modified during that operation then the result is undefined.
Returns |
IntStream | an IntStream of Unicode code points from this sequence |
compareTo
public int compareTo (StringBuffer another)
Compares two StringBuffer
instances lexicographically. This method follows the same rules for lexicographical comparison as defined in the CharSequence.compare(this, another) method.
For finer-grained, locale-sensitive String comparison, refer to Collator
.
Implementation Note:
- This method synchronizes on
this
, the current object, but not StringBuffer another
with which this StringBuffer
is compared.
Parameters |
another | StringBuffer : the StringBuffer to be compared with |
Returns |
int | the value 0 if this StringBuffer contains the same character sequence as that of the argument StringBuffer ; a negative integer if this StringBuffer is lexicographically less than the StringBuffer argument; or a positive integer if this StringBuffer is lexicographically greater than the StringBuffer argument. |
delete
public StringBuffer delete (int start, int end)
Parameters |
start | int |
end | int |
ensureCapacity
public void ensureCapacity (int minimumCapacity)
Parameters |
minimumCapacity | int |
getChars
public void getChars (int srcBegin, int srcEnd, char[] dst, int dstBegin)
Parameters |
srcBegin | int |
srcEnd | int |
dst | char |
dstBegin | int |
indexOf
public int indexOf (String str)
indexOf
public int indexOf (String str, int fromIndex)
Parameters |
str | String |
fromIndex | int |
insert
public StringBuffer insert (int offset, char[] str)
Parameters |
offset | int |
str | char |
insert
public StringBuffer insert (int offset, float f)
Parameters |
offset | int |
f | float |
insert
public StringBuffer insert (int offset, char c)
Parameters |
offset | int |
c | char |
insert
public StringBuffer insert (int offset, long l)
Parameters |
offset | int |
l | long |
insert
public StringBuffer insert (int index, char[] str, int offset, int len)
Parameters |
index | int |
str | char |
offset | int |
len | int |
insert
public StringBuffer insert (int offset, int i)
Parameters |
offset | int |
i | int |
insert
public StringBuffer insert (int offset, String str)
Parameters |
offset | int |
str | String |
insert
public StringBuffer insert (int offset, double d)
Parameters |
offset | int |
d | double |
insert
public StringBuffer insert (int dstOffset, CharSequence s, int start, int end)
Parameters |
dstOffset | int |
s | CharSequence |
start | int |
end | int |
insert
public StringBuffer insert (int offset, boolean b)
Parameters |
offset | int |
b | boolean |
insert
public StringBuffer insert (int offset, Object obj)
Parameters |
offset | int |
obj | Object |
lastIndexOf
public int lastIndexOf (String str, int fromIndex)
Parameters |
str | String |
fromIndex | int |
lastIndexOf
public int lastIndexOf (String str)
length
public int length ()
Returns the length of this character sequence. The length is the number of 16-bit char
s in the sequence.
Returns |
int | the number of char s in this sequence |
offsetByCodePoints
public int offsetByCodePoints (int index, int codePointOffset)
Parameters |
index | int |
codePointOffset | int |
replace
public StringBuffer replace (int start, int end, String str)
Parameters |
start | int |
end | int |
str | String |
setCharAt
public void setCharAt (int index, char ch)
Parameters |
index | int |
ch | char |
setLength
public void setLength (int newLength)
subSequence
public CharSequence subSequence (int start, int end)
Returns a CharSequence
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 char
s) of the returned sequence is end - start
, so if start == end
then an empty sequence is returned.
Parameters |
start | int : the start index, inclusive |
end | int : the end index, exclusive |
substring
public String substring (int start, int end)
Parameters |
start | int |
end | int |
substring
public String substring (int start)
toString
public String toString ()
Returns a string representation of the object.
Returns |
String | a string representation of the object. |
trimToSize
public void trimToSize ()