Class: java.lang.StringBuffer
- 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).
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
-
StringBuffertop
public StringBuffer()Constructs a string buffer with no characters in it and an initial capacity of 16 characters. -
StringBuffertop
public StringBuffer(int capacity)Constructs a string buffer with no characters in it and the specified initial capacity. -
StringBuffertop
public StringBuffer(CharSequence seq)Constructs a string buffer that contains the same characters as the specifiedCharSequence. The initial capacity of the string buffer is16plus the length of theCharSequenceargument.If the length of the specified
CharSequenceis less than or equal to zero, then an empty buffer of capacity16is returned. -
StringBuffertop
public StringBuffer(String str)Constructs a string buffer initialized to the contents of the specified string. The initial capacity of the string buffer is16plus the length of the string argument. -
appendtop
public synchronized StringBuffer append(char c)Appends the specified character to this Appendable.- Specified by:
- append from Appendable
-
appendtop
public synchronized StringBuffer append(double d) -
appendtop
public synchronized StringBuffer append(float f) -
appendtop
public synchronized StringBuffer append(int i) -
appendtop
public synchronized StringBuffer append(long lng) -
appendtop
public StringBuffer append(CharSequence s)Appends the specifiedCharSequenceto this sequence.The characters of the
CharSequenceargument 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
sisnull, then the four characters"null"are appended.- Specified by:
- append from Appendable
-
appendtop
public synchronized 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))- Specified by:
- append from Appendable
-
appendtop
public synchronized StringBuffer append(Object obj) -
appendtop
public synchronized StringBuffer append(String str) -
appendtop
public synchronized 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). -
appendtop
public synchronized StringBuffer append(boolean b) -
appendtop
public synchronized StringBuffer append(char[] str) -
appendtop
public synchronized StringBuffer append(char[] str, int offset, int len) -
appendCodePointtop
public synchronized StringBuffer appendCodePoint(int codePoint) -
capacitytop
public synchronized int capacity() -
charAttop
public synchronized char charAt(int index)Returns thecharvalue at the specified index. An index ranges from zero to length() - 1. The firstcharvalue of the sequence is at index zero, the next at index one, and so on, as for array indexing.If the
charvalue specified by the index is a surrogate, the surrogate value is returned.- Specified by:
- charAt from CharSequence
-
codePointAttop
public synchronized int codePointAt(int index) -
codePointBeforetop
public synchronized int codePointBefore(int index) -
codePointCounttop
public synchronized int codePointCount(int beginIndex, int endIndex) -
deletetop
public synchronized StringBuffer delete(int start, int end) -
deleteCharAttop
public synchronized StringBuffer deleteCharAt(int index) -
ensureCapacitytop
public synchronized void ensureCapacity(int minimumCapacity) -
getCharstop
public synchronized void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) -
indexOftop
public int indexOf(String str) -
indexOftop
public synchronized int indexOf(String str, int fromIndex) -
inserttop
public synchronized StringBuffer insert(int offset, char c) -
inserttop
public StringBuffer insert(int offset, double d) -
inserttop
public StringBuffer insert(int offset, float f) -
inserttop
public StringBuffer insert(int offset, int i) -
inserttop
public StringBuffer insert(int offset, long l) -
inserttop
public StringBuffer insert(int dstOffset, CharSequence s) -
inserttop
public synchronized StringBuffer insert(int dstOffset, CharSequence s, int start, int end) -
inserttop
public synchronized StringBuffer insert(int offset, Object obj) -
inserttop
public synchronized StringBuffer insert(int offset, String str) -
inserttop
public StringBuffer insert(int offset, boolean b) -
inserttop
public synchronized StringBuffer insert(int offset, char[] str) -
inserttop
public synchronized StringBuffer insert(int index, char[] str, int offset, int len) -
lastIndexOftop
public int lastIndexOf(String str) -
lastIndexOftop
public synchronized int lastIndexOf(String str, int fromIndex) -
lengthtop
public synchronized int length()Returns the length of this character sequence. The length is the number of 16-bitchars in the sequence.- Specified by:
- length from CharSequence
-
offsetByCodePointstop
public synchronized int offsetByCodePoints(int index, int codePointOffset) -
replacetop
public synchronized StringBuffer replace(int start, int end, String str) -
reversetop
public synchronized StringBuffer reverse() -
setCharAttop
public synchronized void setCharAt(int index, char ch) -
setLengthtop
public synchronized void setLength(int newLength) -
subSequencetop
public synchronized CharSequence subSequence(int start, int end)Returns a newCharSequencethat is a subsequence of this sequence. The subsequence starts with thecharvalue at the specified index and ends with thecharvalue at index end - 1. The length (inchars) of the returned sequence is end - start, so if start == end then an empty sequence is returned.- Specified by:
- subSequence from CharSequence
-
substringtop
public synchronized String substring(int start) -
substringtop
public synchronized String substring(int start, int end) -
toStringtop
public synchronized String toString()Returns a string containing the characters in this sequence in the same order as this sequence. The length of the string will be the length of this sequence.- Specified by:
- toString from CharSequence
-
trimToSizetop
public synchronized void trimToSize()
