Added in API level 1

StringBuffer

public final class StringBuffer
extends Object implements Appendable, CharSequence, Serializable, Comparable<StringBuffer>

java.lang.Object
   ↳ java.lang.StringBuffer


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(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.

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, Object obj)
StringBuffer insert(int offset, boolean b)
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

Public constructors

StringBuffer

Added in API level 1
public StringBuffer ()

Constructs a string buffer with no characters in it and an initial capacity of 16 characters.

StringBuffer

Added in API level 1
public StringBuffer (int capacity)

Constructs a string buffer with no characters in it and the specified initial capacity.

Parameters
capacity int: the initial capacity.

Throws
NegativeArraySizeException if the capacity argument is less than 0.

StringBuffer

Added in API level 1
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.

StringBuffer

Added in API level 1
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.

Public methods

append

Added in API level 1
public StringBuffer append (boolean b)

Parameters
b boolean

Returns
StringBuffer

append

Added in API level 1
public StringBuffer append (long lng)

Parameters
lng long

Returns
StringBuffer

append

Added in API level 1
public StringBuffer append (char c)

Appends the specified character to this Appendable.

Parameters
c char: The character to append

Returns
StringBuffer A reference to this Appendable

append

Added in API level 1
public StringBuffer append (Object obj)

Parameters
obj Object

Returns
StringBuffer

append

Added in API level 1
public StringBuffer append (char[] str, 
                int offset, 
                int len)

Parameters
str char

offset int

len int

Returns
StringBuffer

Throws
IndexOutOfBoundsException

append

Added in API level 1
public StringBuffer append (double d)

Parameters
d double

Returns
StringBuffer

append

Added in API level 1
public StringBuffer append (char[] str)

Parameters
str char

Returns
StringBuffer

append

Added in API level 1
public StringBuffer append (String str)

Parameters
str String

Returns
StringBuffer

append

Added in API level 1
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.

Returns
StringBuffer a reference to this object.

append

Added in API level 1
public StringBuffer append (float f)

Parameters
f float

Returns
StringBuffer

append

Added in API level 1
public StringBuffer append (int i)

Parameters
i int

Returns
StringBuffer

append

Added in API level 1
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

Returns
StringBuffer A reference to this Appendable

Throws
IndexOutOfBoundsException

append

Added in API level 1
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.

Returns
StringBuffer a reference to this object.

appendCodePoint

Added in API level 1
public StringBuffer appendCodePoint (int codePoint)

Parameters
codePoint int

Returns
StringBuffer

capacity

Added in API level 1
public int capacity ()

Returns
int

charAt

Added in API level 1
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

Throws
IndexOutOfBoundsException

See also:

chars

Added in API level 24
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

Added in API level 1
public int codePointAt (int index)

Parameters
index int

Returns
int

Throws
IndexOutOfBoundsException

codePointBefore

Added in API level 1
public int codePointBefore (int index)

Parameters
index int

Returns
int

Throws
IndexOutOfBoundsException

codePointCount

Added in API level 1
public int codePointCount (int beginIndex, 
                int endIndex)

Parameters
beginIndex int

endIndex int

Returns
int

Throws
IndexOutOfBoundsException

codePoints

Added in API level 24
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

Added in API level 34
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

Added in API level 1
public StringBuffer delete (int start, 
                int end)

Parameters
start int

end int

Returns
StringBuffer

Throws
StringIndexOutOfBoundsException

deleteCharAt

Added in API level 1
public StringBuffer deleteCharAt (int index)

Parameters
index int

Returns
StringBuffer

Throws
StringIndexOutOfBoundsException

ensureCapacity

Added in API level 1
public void ensureCapacity (int minimumCapacity)

Parameters
minimumCapacity int

getChars

Added in API level 1
public void getChars (int srcBegin, 
                int srcEnd, 
                char[] dst, 
                int dstBegin)

Parameters
srcBegin int

srcEnd int

dst char

dstBegin int

Throws
IndexOutOfBoundsException

indexOf

Added in API level 1
public int indexOf (String str)

Parameters
str String

Returns
int

indexOf

Added in API level 1
public int indexOf (String str, 
                int fromIndex)

Parameters
str String

fromIndex int

Returns
int

insert

Added in API level 1
public StringBuffer insert (int offset, 
                char[] str)

Parameters
offset int

str char

Returns
StringBuffer

Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
public StringBuffer insert (int offset, 
                float f)

Parameters
offset int

f float

Returns
StringBuffer

Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
public StringBuffer insert (int dstOffset, 
                CharSequence s)

Parameters
dstOffset int

s CharSequence

Returns
StringBuffer

Throws
IndexOutOfBoundsException

insert

Added in API level 1
public StringBuffer insert (int offset, 
                char c)

Parameters
offset int

c char

Returns
StringBuffer

Throws
IndexOutOfBoundsException

insert

Added in API level 1
public StringBuffer insert (int offset, 
                long l)

Parameters
offset int

l long

Returns
StringBuffer

Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
public StringBuffer insert (int index, 
                char[] str, 
                int offset, 
                int len)

Parameters
index int

str char

offset int

len int

Returns
StringBuffer

Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
public StringBuffer insert (int offset, 
                int i)

Parameters
offset int

i int

Returns
StringBuffer

Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
public StringBuffer insert (int offset, 
                String str)

Parameters
offset int

str String

Returns
StringBuffer

Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
public StringBuffer insert (int offset, 
                double d)

Parameters
offset int

d double

Returns
StringBuffer

Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
public StringBuffer insert (int dstOffset, 
                CharSequence s, 
                int start, 
                int end)

Parameters
dstOffset int

s CharSequence

start int

end int

Returns
StringBuffer

Throws
IndexOutOfBoundsException

insert

Added in API level 1
public StringBuffer insert (int offset, 
                Object obj)

Parameters
offset int

obj Object

Returns
StringBuffer

Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
public StringBuffer insert (int offset, 
                boolean b)

Parameters
offset int

b boolean

Returns
StringBuffer

Throws
StringIndexOutOfBoundsException

lastIndexOf

Added in API level 1
public int lastIndexOf (String str, 
                int fromIndex)

Parameters
str String

fromIndex int

Returns
int

lastIndexOf

Added in API level 1
public int lastIndexOf (String str)

Parameters
str String

Returns
int

length

Added in API level 1
public int length ()

Returns the length of this character sequence. The length is the number of 16-bit chars in the sequence.

Returns
int the number of chars in this sequence

offsetByCodePoints

Added in API level 1
public int offsetByCodePoints (int index, 
                int codePointOffset)

Parameters
index int

codePointOffset int

Returns
int

Throws
IndexOutOfBoundsException

replace

Added in API level 1
public StringBuffer replace (int start, 
                int end, 
                String str)

Parameters
start int

end int

str String

Returns
StringBuffer

Throws
StringIndexOutOfBoundsException

reverse

Added in API level 1
public StringBuffer reverse ()

Returns
StringBuffer

setCharAt

Added in API level 1
public void setCharAt (int index, 
                char ch)

Parameters
index int

ch char

Throws
IndexOutOfBoundsException

See also:

setLength

Added in API level 1
public void setLength (int newLength)

Parameters
newLength int

Throws
IndexOutOfBoundsException

See also:

subSequence

Added in API level 1
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 chars) 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

Returns
CharSequence the specified subsequence

Throws
IndexOutOfBoundsException

substring

Added in API level 1
public String substring (int start, 
                int end)

Parameters
start int

end int

Returns
String

Throws
StringIndexOutOfBoundsException

substring

Added in API level 1
public String substring (int start)

Parameters
start int

Returns
String

Throws
StringIndexOutOfBoundsException

toString

Added in API level 1
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.

trimToSize

Added in API level 1
public void trimToSize ()