java.lang

Class StringBuffer

public final class StringBuffer 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).

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

Author: Arthur van Hoff

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
StringBufferappend(Object obj)
StringBufferappend(String str)
StringBufferappend(StringBuffer sb)
Appends the specified StringBuffer to this sequence.
StringBufferappend(CharSequence s)
Appends the specified CharSequence to this sequence.
StringBufferappend(CharSequence s, int srcOffset, int len)
Appends a subsequence of the specified CharSequence to this sequence.
StringBufferappend(char[] str)
StringBufferappend(char[] str, int offset, int len)
StringBufferappend(boolean b)
StringBufferappend(char c)
StringBufferappend(int i)
StringBufferappend(long lng)
StringBufferappend(float f)
StringBufferappend(double d)
intcapacity()
charcharAt(int index)
StringBufferdelete(int start, int end)
StringBufferdeleteCharAt(int index)
voidensureCapacity(int minimumCapacity)
voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
intindexOf(String str)
intindexOf(String str, int fromIndex)
StringBufferinsert(int index, char[] str, int offset, int len)
StringBufferinsert(int offset, Object obj)
StringBufferinsert(int offset, String str)
StringBufferinsert(int offset, char[] str)
StringBufferinsert(int dstOffset, CharSequence s)
StringBufferinsert(int dstOffset, CharSequence s, int srcOffset, int len)
StringBufferinsert(int offset, boolean b)
StringBufferinsert(int offset, char c)
StringBufferinsert(int offset, int i)
StringBufferinsert(int offset, long l)
StringBufferinsert(int offset, float f)
StringBufferinsert(int offset, double d)
intlastIndexOf(String str)
intlastIndexOf(String str, int fromIndex)
intlength()
StringBufferreplace(int start, int end, String str)
StringBufferreverse()
voidsetCharAt(int index, char ch)
voidsetLength(int newLength)
CharSequencesubSequence(int start, int end)
Stringsubstring(int start)
Stringsubstring(int start, int end)
StringtoString()
voidtrimToSize()

Constructor Detail

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 the initial capacity.

Throws: NegativeArraySizeException if the capacity argument is less than 0.

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 the initial contents of the buffer.

Throws: NullPointerException if str is null

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 the sequence to copy.

Throws: NullPointerException if seq is null

Since: 1.5

Method Detail

append

public StringBuffer append(Object obj)

See Also: valueOf append

append

public StringBuffer append(String str)

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 the StringBuffer to append.

Returns: a reference to this object.

Since: 1.4

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 the CharSequence to append.

Returns: a reference to this object.

Since: 1.5

append

public StringBuffer append(CharSequence s, int srcOffset, int len)
Appends a subsequence of the specified 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

append

public StringBuffer append(char[] str)

append

public StringBuffer append(char[] str, int offset, int len)

append

public StringBuffer append(boolean b)

See Also: String append

append

public StringBuffer append(char c)

append

public StringBuffer append(int i)

See Also: String append

append

public StringBuffer append(long lng)

See Also: String append

append

public StringBuffer append(float f)

See Also: String append

append

public StringBuffer append(double d)

See Also: String append

capacity

public int capacity()

charAt

public char charAt(int index)

See Also: length

delete

public StringBuffer delete(int start, int end)

Since: 1.2

deleteCharAt

public StringBuffer deleteCharAt(int index)

Since: 1.2

ensureCapacity

public void ensureCapacity(int minimumCapacity)

getChars

public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

indexOf

public int indexOf(String str)

Since: 1.4

indexOf

public int indexOf(String str, int fromIndex)

Since: 1.4

insert

public StringBuffer insert(int index, char[] str, int offset, int len)

Since: 1.2

insert

public StringBuffer insert(int offset, Object obj)

See Also: valueOf StringBuffer length

insert

public StringBuffer insert(int offset, String str)

See Also: length

insert

public StringBuffer insert(int offset, char[] str)

insert

public StringBuffer insert(int dstOffset, CharSequence s)

Since: 1.5

insert

public StringBuffer insert(int dstOffset, CharSequence s, int srcOffset, int len)

Since: 1.5

insert

public StringBuffer insert(int offset, boolean b)

See Also: String StringBuffer length

insert

public StringBuffer insert(int offset, char c)

See Also: length

insert

public StringBuffer insert(int offset, int i)

See Also: String StringBuffer length

insert

public StringBuffer insert(int offset, long l)

See Also: String StringBuffer length

insert

public StringBuffer insert(int offset, float f)

See Also: String StringBuffer length

insert

public StringBuffer insert(int offset, double d)

See Also: String StringBuffer length

lastIndexOf

public int lastIndexOf(String str)

Since: 1.4

lastIndexOf

public int lastIndexOf(String str, int fromIndex)

Since: 1.4

length

public int length()

replace

public StringBuffer replace(int start, int end, String str)

Since: 1.2

reverse

public StringBuffer reverse()

Since: JDK1.0.2

setCharAt

public void setCharAt(int index, char ch)

See Also: length

setLength

public void setLength(int newLength)

See Also: length

subSequence

public CharSequence subSequence(int start, int end)

Since: 1.4

substring

public String substring(int start)

Since: 1.2

substring

public String substring(int start, int end)

Since: 1.2

toString

public String toString()

trimToSize

public void trimToSize()

Since: 1.5