| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
StringBuffer sb = new StringBuffer();
Based on 64 examples
public final class StringBuffer extends AbstractStringBuilder implements Serializable, CharSequence
A thread-safe, mutable sequence of characters. A string buffer is like a {@link 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, {@link 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.
Constructor Summary | |
---|---|
Constructs a string buffer with no characters in it and an initial capacity of 16 characters. |
|
StringBuffer(CharSequence seq) Constructs a string buffer that contains the same characters as the specified CharSequence .
|
|
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. |
Method Summary | |
---|---|
StringBuffer |
append(boolean b) |
StringBuffer |
append(char c) |
StringBuffer |
Appends the specified CharSequence to this
sequence.
|
StringBuffer |
append(CharSequence s, int start, int end) |
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 |
|
StringBuffer |
|
StringBuffer |
append(StringBuffer sb) Appends the specified StringBuffer to this sequence. |
StringBuffer |
appendCodePoint(int codePoint) |
int |
capacity() Returns the current capacity. |
char |
charAt(int index) Returns the char value in this sequence at the specified index.
|
int |
codePointAt(int index) Returns the character (Unicode code point) at the specified index. |
int |
codePointBefore(int index) Returns the character (Unicode code point) before the specified index. |
int |
codePointCount(int beginIndex, int endIndex) Returns the number of Unicode code points in the specified text range of this sequence. |
StringBuffer |
delete(int start, int end) |
StringBuffer |
deleteCharAt(int index) |
void |
ensureCapacity(int minimumCapacity) Ensures that the capacity is at least equal to the specified minimum. |
void |
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Characters are copied from this sequence into the destination character array dst .
|
int |
|
int |
|
StringBuffer |
insert(int offset, boolean b) |
StringBuffer |
insert(int offset, char c) |
StringBuffer |
insert(int dstOffset, CharSequence s) |
StringBuffer |
insert(int dstOffset, CharSequence s, int start, int end) |
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 |
|
StringBuffer |
|
int |
lastIndexOf(String str) |
int |
lastIndexOf(String str, int fromIndex) |
int |
length() Returns the length (character count). |
int |
offsetByCodePoints(int index, int codePointOffset) Returns the index within this sequence that is offset from the given index by codePointOffset code
points.
|
StringBuffer |
|
StringBuffer |
reverse() |
void |
setCharAt(int index, char ch) The character at the specified index is set to ch .
|
void |
setLength(int newLength) Sets the length of the character sequence. |
CharSequence |
subSequence(int start, int end) Returns a new character sequence that is a subsequence of this sequence. |
String |
substring(int start) Returns a new String that contains a subsequence of
characters currently contained in this character sequence.
|
String |
substring(int start, int end) Returns a new String that contains a subsequence of
characters currently contained in this character sequence.
|
String |
toString() |
void |
Attempts to reduce storage used for the character sequence. |
Constructor Detail |
---|
public StringBuffer()
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 StringBuffer(int capacity)
capacity
- the initial capacity.public StringBuffer(String str)
16
plus the length of the string argument.
str
- the initial contents of the buffer.Method Detail |
---|
public synchronized StringBuffer append(boolean b)
append
in class AbstractStringBuilder
b
public synchronized StringBuffer append(char c)
append
in class AbstractStringBuilder
c
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
in class AbstractStringBuilder
s
- the CharSequence
to append.public synchronized StringBuffer append(CharSequence s, int start, int end)
append
in class AbstractStringBuilder
s
start
end
public synchronized StringBuffer append(char[] str)
append
in class AbstractStringBuilder
str
public synchronized StringBuffer append(char[] str, int offset, int len)
append
in class AbstractStringBuilder
str
offset
len
public synchronized StringBuffer append(double d)
append
in class AbstractStringBuilder
d
public synchronized StringBuffer append(float f)
append
in class AbstractStringBuilder
f
public synchronized StringBuffer append(int i)
append
in class AbstractStringBuilder
i
public synchronized StringBuffer append(long lng)
append
in class AbstractStringBuilder
lng
public synchronized StringBuffer append(Object obj)
append
in class AbstractStringBuilder
obj
public synchronized StringBuffer append(String str)
append
in class AbstractStringBuilder
str
public synchronized StringBuffer append(StringBuffer sb)
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
).
append
in class AbstractStringBuilder
sb
- the StringBuffer to append.public synchronized StringBuffer appendCodePoint(int codePoint)
appendCodePoint
in class AbstractStringBuilder
codePoint
public synchronized int capacity()
capacity
in class AbstractStringBuilder
public synchronized char charAt(int index)
charAt
in class AbstractStringBuilder
index
public synchronized int codePointAt(int index)
codePointAt
in class AbstractStringBuilder
index
public synchronized int codePointBefore(int index)
codePointBefore
in class AbstractStringBuilder
index
public synchronized int codePointCount(int beginIndex, int endIndex)
codePointCount
in class AbstractStringBuilder
beginIndex
endIndex
public synchronized StringBuffer delete(int start, int end)
delete
in class AbstractStringBuilder
start
end
public synchronized StringBuffer deleteCharAt(int index)
deleteCharAt
in class AbstractStringBuilder
index
public synchronized void ensureCapacity(int minimumCapacity)
ensureCapacity
in class AbstractStringBuilder
minimumCapacity
public synchronized void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
getChars
in class AbstractStringBuilder
srcBegin
srcEnd
dst
dstBegin
public int indexOf(String str)
indexOf
in class AbstractStringBuilder
str
public synchronized int indexOf(String str, int fromIndex)
indexOf
in class AbstractStringBuilder
str
fromIndex
public StringBuffer insert(int offset, boolean b)
insert
in class AbstractStringBuilder
offset
b
public synchronized StringBuffer insert(int offset, char c)
insert
in class AbstractStringBuilder
offset
c
public StringBuffer insert(int dstOffset, CharSequence s)
insert
in class AbstractStringBuilder
dstOffset
s
public synchronized StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
insert
in class AbstractStringBuilder
dstOffset
s
start
end
public synchronized StringBuffer insert(int offset, char[] str)
insert
in class AbstractStringBuilder
offset
str
public synchronized StringBuffer insert(int index, char[] str, int offset, int len)
insert
in class AbstractStringBuilder
index
str
offset
len
public StringBuffer insert(int offset, double d)
insert
in class AbstractStringBuilder
offset
d
public StringBuffer insert(int offset, float f)
insert
in class AbstractStringBuilder
offset
f
public StringBuffer insert(int offset, int i)
insert
in class AbstractStringBuilder
offset
i
public StringBuffer insert(int offset, long l)
insert
in class AbstractStringBuilder
offset
l
public synchronized StringBuffer insert(int offset, Object obj)
insert
in class AbstractStringBuilder
offset
obj
public synchronized StringBuffer insert(int offset, String str)
insert
in class AbstractStringBuilder
offset
str
public int lastIndexOf(String str)
lastIndexOf
in class AbstractStringBuilder
str
public synchronized int lastIndexOf(String str, int fromIndex)
lastIndexOf
in class AbstractStringBuilder
str
fromIndex
public synchronized int length()
length
in class AbstractStringBuilder
public synchronized int offsetByCodePoints(int index, int codePointOffset)
offsetByCodePoints
in class AbstractStringBuilder
index
codePointOffset
public synchronized StringBuffer replace(int start, int end, String str)
replace
in class AbstractStringBuilder
start
end
str
public synchronized StringBuffer reverse()
reverse
in class AbstractStringBuilder
public synchronized void setCharAt(int index, char ch)
setCharAt
in class AbstractStringBuilder
index
ch
public synchronized void setLength(int newLength)
setLength
in class AbstractStringBuilder
newLength
public synchronized CharSequence subSequence(int start, int end)
subSequence
in class AbstractStringBuilder
start
end
public synchronized String substring(int start)
substring
in class AbstractStringBuilder
start
public synchronized String substring(int start, int end)
substring
in class AbstractStringBuilder
start
end
public synchronized String toString()
toString
in class AbstractStringBuilder
public synchronized void trimToSize()
trimToSize
in class AbstractStringBuilder
| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |