public final class java.lang
StringBuffer


Hide details Login
Java SE 6
  
Extends: AbstractStringBuilder
Implements: Serializable, CharSequence

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.

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.
version1.101, 11/17/05
sinceJDK1.0
See also java.lang.StringBuilder, java.lang.String


Constructors
public StringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
public StringBuffer(int capacity)
Constructs a string buffer with no characters in it and the specified initial capacity.
capacitythe initial capacity.
ThrowsNegativeArraySizeException: if the capacity argument is less than 0.
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.
strthe initial contents of the buffer.
ThrowsNullPointerException: if str is null
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.
seqthe sequence to copy.
ThrowsNullPointerException: if seq is null
since1.5


Methods
public StringBuffer append(Object obj)
See also valueOf(java.lang.Object), append(java.lang.String)
public StringBuffer append(String str)
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).
sbthe StringBuffer to append.
returna reference to this object.
since1.4

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.
sthe CharSequence to append.
returna reference to this object.
since1.5

public StringBuffer append(CharSequence s, int start, int end)
ThrowsIndexOutOfBoundsException: if start or end are negative, or start is greater than end or end is greater than s.length()
since1.5
public StringBuffer append(char[] str)
public StringBuffer append(char[] str, int offset, int len)
public StringBuffer append(boolean b)
See also valueOf(boolean), append(java.lang.String)
public StringBuffer append(char c)
public StringBuffer append(int i)
See also valueOf(int), append(java.lang.String)
public StringBuffer append(long lng)
See also valueOf(long), append(java.lang.String)
public StringBuffer append(float f)
See also valueOf(float), append(java.lang.String)
public StringBuffer append(double d)
See also valueOf(double), append(java.lang.String)
public StringBuffer appendCodePoint(int codePoint)
since1.5
public int capacity()
public char charAt(int index)
ThrowsIndexOutOfBoundsException: if the index argument is negative or not less than length()
See also length()
public int codePointAt(int index)
since1.5
public int codePointBefore(int index)
since1.5
public int codePointCount(int beginIndex, int endIndex)
since1.5
public StringBuffer delete(int start, int end)
ThrowsStringIndexOutOfBoundsException: if start is negative, greater than length(), or greater than end.
since1.2
public StringBuffer deleteCharAt(int index)
ThrowsStringIndexOutOfBoundsException: if the index is negative or greater than or equal to length().
since1.2
public void ensureCapacity(int minimumCapacity)
public int indexOf(String str)
ThrowsNullPointerException: if str is null.
since1.4
public int indexOf(String str, int fromIndex)
ThrowsNullPointerException: if str is null.
since1.4
public StringBuffer insert(int index, char[] str, int offset, int len)
ThrowsStringIndexOutOfBoundsException: if index is negative or greater than length(), or offset or len are negative, or (offset+len) is greater than str.length.
since1.2
public StringBuffer insert(int offset, Object obj)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also valueOf(java.lang.Object), insert(int, java.lang.String), length()
public StringBuffer insert(int offset, String str)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also length()
public StringBuffer insert(int offset, char[] str)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
public StringBuffer insert(int dstOffset, CharSequence s)
ThrowsIndexOutOfBoundsException: if the offset is invalid.
since1.5
public StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
ThrowsIndexOutOfBoundsException: if dstOffset is negative or greater than this.length(), or start or end are negative, or start is greater than end or end is greater than s.length()
since1.5
public StringBuffer insert(int offset, boolean b)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also valueOf(boolean), insert(int, java.lang.String), length()
public StringBuffer insert(int offset, char c)
ThrowsIndexOutOfBoundsException: if the offset is invalid.
See also length()
public StringBuffer insert(int offset, int i)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also valueOf(int), insert(int, java.lang.String), length()
public StringBuffer insert(int offset, long l)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also valueOf(long), insert(int, java.lang.String), length()
public StringBuffer insert(int offset, float f)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also valueOf(float), insert(int, java.lang.String), length()
public StringBuffer insert(int offset, double d)
ThrowsStringIndexOutOfBoundsException: if the offset is invalid.
See also valueOf(double), insert(int, java.lang.String), length()
public int lastIndexOf(String str)
ThrowsNullPointerException: if str is null.
since1.4
public int lastIndexOf(String str, int fromIndex)
ThrowsNullPointerException: if str is null.
since1.4
public int length()
public int offsetByCodePoints(int index, int codePointOffset)
since1.5
public StringBuffer replace(int start, int end, String str)
ThrowsStringIndexOutOfBoundsException: if start is negative, greater than length(), or greater than end.
since1.2
public StringBuffer reverse()
sinceJDK1.0.2
public CharSequence subSequence(int start, int end)
ThrowsIndexOutOfBoundsException: if start or end are negative, if end is greater than length(), or if start is greater than end
since1.4
public String substring(int start)
ThrowsStringIndexOutOfBoundsException: if start is less than zero, or greater than the length of this object.
since1.2
public String substring(int start, int end)
ThrowsStringIndexOutOfBoundsException: if start or end are negative or greater than length(), or start is greater than end.
since1.2
public String toString()
public void trimToSize()
since1.5

Properties
public void setCharAt(int index, char ch)
ThrowsIndexOutOfBoundsException: if index is negative or greater than or equal to length().
See also length()
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
ThrowsNullPointerException: if dst is null.
ThrowsIndexOutOfBoundsException: if any of the following is true:
  • srcBegin is negative
  • dstBegin is negative
  • the srcBegin argument is greater than the srcEnd argument.
  • srcEnd is greater than this.length().
  • dstBegin+srcEnd-srcBegin is greater than dst.length
public void setLength(int newLength)
ThrowsIndexOutOfBoundsException: if the newLength argument is negative.
See also length()