[proxy] web.archive.org← back | site home | direct (HTTPS) ↗ | proxy home | ◑ dark◐ light

StringBuffer  |  Android Developers

public final class StringBuffer
extends Object implements Appendable, CharSequence, Serializable



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.

int codePointAt(int index)
int codePointBefore(int index)
int codePointCount(int beginIndex, int endIndex)
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

From class java.lang.Object

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeout, int nanos)

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

final void wait(long timeout)

Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

final void wait()

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

From interface java.lang.Appendable

abstract Appendable append(char c)

Appends the specified character to this Appendable.

abstract Appendable append(CharSequence csq, int start, int end)

Appends a subsequence of the specified character sequence to this Appendable.

abstract Appendable append(CharSequence csq)

Appends the specified character sequence to this Appendable.

From interface java.lang.CharSequence

abstract char charAt(int index)

Returns the char value at the specified index.

default IntStream chars()

Returns a stream of int zero-extending the char values from this sequence.

default IntStream codePoints()

Returns a stream of code point values from this sequence.

abstract int length()

Returns the length of this character sequence.

abstract CharSequence subSequence(int start, int end)

Returns a CharSequence that is a subsequence of this sequence.

abstract String toString()

Returns a string containing the characters in this sequence in the same order as this sequence.

Public constructors

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

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

Public methods

append

public StringBuffer append (boolean b)
Parameters
b boolean
Returns
StringBuffer

append

public StringBuffer append (long lng)
Parameters
lng long
Returns
StringBuffer

append

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

public StringBuffer append (Object obj)
Parameters
obj Object
Returns
StringBuffer

append

public StringBuffer append (char[] str, 
                int offset, 
                int len)
Parameters
str char
offset int
len int
Returns
StringBuffer
Throws
IndexOutOfBoundsException

append

public StringBuffer append (double d)
Parameters
d double
Returns
StringBuffer

append

public StringBuffer append (char[] str)
Parameters
str char
Returns
StringBuffer

append

public StringBuffer append (String str)
Parameters
str String
Returns
StringBuffer

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 StringBuffer: the StringBuffer to append.
Returns
StringBuffer a reference to this object.

append

public StringBuffer append (float f)
Parameters
f float
Returns
StringBuffer

append

public StringBuffer append (int i)
Parameters
i int
Returns
StringBuffer

append

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

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

public StringBuffer appendCodePoint (int codePoint)
Parameters
codePoint int
Returns
StringBuffer

capacity

public int capacity ()
Returns
int

charAt

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

codePointAt

public int codePointAt (int index)
Parameters
index int
Returns
int

codePointBefore

public int codePointBefore (int index)
Parameters
index int
Returns
int

codePointCount

public int codePointCount (int beginIndex, 
                int endIndex)
Parameters
beginIndex int
endIndex int
Returns
int

delete

public StringBuffer delete (int start, 
                int end)
Parameters
start int
end int
Returns
StringBuffer
Throws
StringIndexOutOfBoundsException

deleteCharAt

public StringBuffer deleteCharAt (int index)
Parameters
index int
Returns
StringBuffer
Throws
StringIndexOutOfBoundsException

ensureCapacity

public void ensureCapacity (int minimumCapacity)
Parameters
minimumCapacity int

getChars

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

indexOf

public int indexOf (String str)
Parameters
str String
Returns
int

indexOf

public int indexOf (String str, 
                int fromIndex)
Parameters
str String
fromIndex int
Returns
int

insert

public StringBuffer insert (int offset, 
                char[] str)
Parameters
offset int
str char
Returns
StringBuffer
Throws
StringIndexOutOfBoundsException

insert

public StringBuffer insert (int offset, 
                float f)
Parameters
offset int
f float
Returns
StringBuffer
Throws
StringIndexOutOfBoundsException

insert

public StringBuffer insert (int dstOffset, 
                CharSequence s)
Parameters
dstOffset int
s CharSequence
Returns
StringBuffer
Throws
IndexOutOfBoundsException

insert

public StringBuffer insert (int offset, 
                char c)
Parameters
offset int
c char
Returns
StringBuffer
Throws
IndexOutOfBoundsException

insert

public StringBuffer insert (int offset, 
                long l)
Parameters
offset int
l long
Returns
StringBuffer
Throws
StringIndexOutOfBoundsException

insert

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

public StringBuffer insert (int offset, 
                int i)
Parameters
offset int
i int
Returns
StringBuffer
Throws
StringIndexOutOfBoundsException

insert

public StringBuffer insert (int offset, 
                String str)
Parameters
offset int
str String
Returns
StringBuffer
Throws
StringIndexOutOfBoundsException

insert

public StringBuffer insert (int offset, 
                double d)
Parameters
offset int
d double
Returns
StringBuffer
Throws
StringIndexOutOfBoundsException

insert

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

public StringBuffer insert (int offset, 
                Object obj)
Parameters
offset int
obj Object
Returns
StringBuffer
Throws
StringIndexOutOfBoundsException

insert

public StringBuffer insert (int offset, 
                boolean b)
Parameters
offset int
b boolean
Returns
StringBuffer
Throws
StringIndexOutOfBoundsException

lastIndexOf

public int lastIndexOf (String str, 
                int fromIndex)
Parameters
str String
fromIndex int
Returns
int

lastIndexOf

public int lastIndexOf (String str)
Parameters
str String
Returns
int

length

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

public int offsetByCodePoints (int index, 
                int codePointOffset)
Parameters
index int
codePointOffset int
Returns
int

replace

public StringBuffer replace (int start, 
                int end, 
                String str)
Parameters
start int
end int
str String
Returns
StringBuffer
Throws
StringIndexOutOfBoundsException

reverse

public StringBuffer reverse ()
Returns
StringBuffer

setCharAt

public void setCharAt (int index, 
                char ch)
Parameters
index int
ch char
Throws
IndexOutOfBoundsException

setLength

public void setLength (int newLength)
Parameters
newLength int
Throws
IndexOutOfBoundsException

subSequence

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

public String substring (int start, 
                int end)
Parameters
start int
end int
Returns
String
Throws
StringIndexOutOfBoundsException

substring

public String substring (int start)
Parameters
start int
Returns
String
Throws
StringIndexOutOfBoundsException

toString

public String toString ()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 
Returns
String a string representation of the object.

trimToSize

public void trimToSize ()