This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.


java.lang
class StringBuffer

java.lang.StringBuffer
All Implemented Interfaces:
Serializable, CharSequence

Most common way to construct:

StringBuffer sb = new StringBuffer();

Based on 64 examples


public final class StringBuffer
extends AbstractStringBuilder
implements Serializable, CharSequence

A thread-safe, mutable sequence of characters. A string buffer is like a {@link 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, {@link 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.


Constructor Summary

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

          Constructs a string buffer that contains the same characters as the specified CharSequence.
StringBuffer(int capacity)

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

          Constructs a string buffer initialized to the contents of the specified string.
 
Method Summary
 StringBuffer
append(boolean b)

          
 StringBuffer
append(char c)

          
 StringBuffer

          Appends the specified CharSequence to this sequence.
 StringBuffer
append(CharSequence s, int start, int end)

          
 StringBuffer
append(char[] str)

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

          
 StringBuffer
append(double d)

          
 StringBuffer
append(float f)

          
 StringBuffer
append(int i)

          
 StringBuffer
append(long lng)

          
 StringBuffer

          
 StringBuffer

          
 StringBuffer

          Appends the specified StringBuffer to this sequence.
 StringBuffer
appendCodePoint(int codePoint)

          
 int

          Returns the current capacity.
 char
charAt(int index)

          Returns the char value in this sequence at the specified index.
 int
codePointAt(int index)

          Returns the character (Unicode code point) at the specified index.
 int
codePointBefore(int index)

          Returns the character (Unicode code point) before the specified index.
 int
codePointCount(int beginIndex, int endIndex)

          Returns the number of Unicode code points in the specified text range of this sequence.
 StringBuffer
delete(int start, int end)

          
 StringBuffer
deleteCharAt(int index)

          
 void
ensureCapacity(int minimumCapacity)

          Ensures that the capacity is at least equal to the specified minimum.
 void
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

          Characters are copied from this sequence into the destination character array dst.
 int

          
 int
indexOf(String str, int fromIndex)

          
 StringBuffer
insert(int offset, boolean b)

          
 StringBuffer
insert(int offset, char c)

          
 StringBuffer
insert(int dstOffset, CharSequence s)

          
 StringBuffer
insert(int dstOffset, CharSequence s, int start, int end)

          
 StringBuffer
insert(int offset, char[] str)

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

          
 StringBuffer
insert(int offset, double d)

          
 StringBuffer
insert(int offset, float f)

          
 StringBuffer
insert(int offset, int i)

          
 StringBuffer
insert(int offset, long l)

          
 StringBuffer
insert(int offset, Object obj)

          
 StringBuffer
insert(int offset, String str)

          
 int

          
 int
lastIndexOf(String str, int fromIndex)

          
 int

          Returns the length (character count).
 int
offsetByCodePoints(int index, int codePointOffset)

          Returns the index within this sequence that is offset from the given index by codePointOffset code points.
 StringBuffer
replace(int start, int end, String str)

          
 StringBuffer

          
 void
setCharAt(int index, char ch)

          The character at the specified index is set to ch.
 void
setLength(int newLength)

          Sets the length of the character sequence.
 CharSequence
subSequence(int start, int end)

          Returns a new character sequence that is a subsequence of this sequence.
 String
substring(int start)

          Returns a new String that contains a subsequence of characters currently contained in this character sequence.
 String
substring(int start, int end)

          Returns a new String that contains a subsequence of characters currently contained in this character sequence.
 String

          
 void

          Attempts to reduce storage used for the character sequence.
 

Constructor Detail

StringBuffer

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


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.

StringBuffer

public StringBuffer(int capacity)
Constructs a string buffer with no characters in it and the specified initial capacity.

Parameters:
capacity - the initial capacity.

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.
Method Detail

append

public synchronized StringBuffer append(boolean b)
Overrides:
append in class AbstractStringBuilder
Parameters:
b

append

public synchronized StringBuffer append(char c)
Overrides:
append in class AbstractStringBuilder
Parameters:
c

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.

Overrides:
append in class AbstractStringBuilder
Parameters:
s - the CharSequence to append.
Returns:
a reference to this object.

append

public synchronized StringBuffer append(CharSequence s,
                                        int start,
                                        int end)
Overrides:
append in class AbstractStringBuilder
Parameters:
s
start
end

append

public synchronized StringBuffer append(char[] str)
Overrides:
append in class AbstractStringBuilder
Parameters:
str

append

public synchronized StringBuffer append(char[] str,
                                        int offset,
                                        int len)
Overrides:
append in class AbstractStringBuilder
Parameters:
str
offset
len

append

public synchronized StringBuffer append(double d)
Overrides:
append in class AbstractStringBuilder
Parameters:
d

append

public synchronized StringBuffer append(float f)
Overrides:
append in class AbstractStringBuilder
Parameters:
f

append

public synchronized StringBuffer append(int i)
Overrides:
append in class AbstractStringBuilder
Parameters:
i

append

public synchronized StringBuffer append(long lng)
Overrides:
append in class AbstractStringBuilder
Parameters:
lng

append

public synchronized StringBuffer append(Object obj)
Overrides:
append in class AbstractStringBuilder
Parameters:
obj

append

public synchronized StringBuffer append(String str)
Overrides:
append in class AbstractStringBuilder
Parameters:
str

append

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

Overrides:
append in class AbstractStringBuilder
Parameters:
sb - the StringBuffer to append.
Returns:
a reference to this object.

appendCodePoint

public synchronized StringBuffer appendCodePoint(int codePoint)
Overrides:
appendCodePoint in class AbstractStringBuilder
Parameters:
codePoint

capacity

public synchronized int capacity()
Overrides:
capacity in class AbstractStringBuilder

charAt

public synchronized char charAt(int index)
Overrides:
charAt in class AbstractStringBuilder
Parameters:
index

codePointAt

public synchronized int codePointAt(int index)
Overrides:
codePointAt in class AbstractStringBuilder
Parameters:
index

codePointBefore

public synchronized int codePointBefore(int index)
Overrides:
codePointBefore in class AbstractStringBuilder
Parameters:
index

codePointCount

public synchronized int codePointCount(int beginIndex,
                                       int endIndex)
Overrides:
codePointCount in class AbstractStringBuilder
Parameters:
beginIndex
endIndex

delete

public synchronized StringBuffer delete(int start,
                                        int end)
Overrides:
delete in class AbstractStringBuilder
Parameters:
start
end

deleteCharAt

public synchronized StringBuffer deleteCharAt(int index)
Overrides:
deleteCharAt in class AbstractStringBuilder
Parameters:
index

ensureCapacity

public synchronized void ensureCapacity(int minimumCapacity)
Overrides:
ensureCapacity in class AbstractStringBuilder
Parameters:
minimumCapacity

getChars

public synchronized void getChars(int srcBegin,
                                  int srcEnd,
                                  char[] dst,
                                  int dstBegin)
Overrides:
getChars in class AbstractStringBuilder
Parameters:
srcBegin
srcEnd
dst
dstBegin

indexOf

public int indexOf(String str)
Overrides:
indexOf in class AbstractStringBuilder
Parameters:
str

indexOf

public synchronized int indexOf(String str,
                                int fromIndex)
Overrides:
indexOf in class AbstractStringBuilder
Parameters:
str
fromIndex

insert

public StringBuffer insert(int offset,
                           boolean b)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
b

insert

public synchronized StringBuffer insert(int offset,
                                        char c)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
c

insert

public StringBuffer insert(int dstOffset,
                           CharSequence s)
Overrides:
insert in class AbstractStringBuilder
Parameters:
dstOffset
s

insert

public synchronized StringBuffer insert(int dstOffset,
                                        CharSequence s,
                                        int start,
                                        int end)
Overrides:
insert in class AbstractStringBuilder
Parameters:
dstOffset
s
start
end

insert

public synchronized StringBuffer insert(int offset,
                                        char[] str)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
str

insert

public synchronized StringBuffer insert(int index,
                                        char[] str,
                                        int offset,
                                        int len)
Overrides:
insert in class AbstractStringBuilder
Parameters:
index
str
offset
len

insert

public StringBuffer insert(int offset,
                           double d)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
d

insert

public StringBuffer insert(int offset,
                           float f)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
f

insert

public StringBuffer insert(int offset,
                           int i)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
i

insert

public StringBuffer insert(int offset,
                           long l)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
l

insert

public synchronized StringBuffer insert(int offset,
                                        Object obj)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
obj

insert

public synchronized StringBuffer insert(int offset,
                                        String str)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
str

lastIndexOf

public int lastIndexOf(String str)
Overrides:
lastIndexOf in class AbstractStringBuilder
Parameters:
str

lastIndexOf

public synchronized int lastIndexOf(String str,
                                    int fromIndex)
Overrides:
lastIndexOf in class AbstractStringBuilder
Parameters:
str
fromIndex

length

public synchronized int length()
Overrides:
length in class AbstractStringBuilder

offsetByCodePoints

public synchronized int offsetByCodePoints(int index,
                                           int codePointOffset)
Overrides:
offsetByCodePoints in class AbstractStringBuilder
Parameters:
index
codePointOffset

replace

public synchronized StringBuffer replace(int start,
                                         int end,
                                         String str)
Overrides:
replace in class AbstractStringBuilder
Parameters:
start
end
str

reverse

public synchronized StringBuffer reverse()
Overrides:
reverse in class AbstractStringBuilder

setCharAt

public synchronized void setCharAt(int index,
                                   char ch)
Overrides:
setCharAt in class AbstractStringBuilder
Parameters:
index
ch

setLength

public synchronized void setLength(int newLength)
Overrides:
setLength in class AbstractStringBuilder
Parameters:
newLength

subSequence

public synchronized CharSequence subSequence(int start,
                                             int end)
Overrides:
subSequence in class AbstractStringBuilder
Parameters:
start
end

substring

public synchronized String substring(int start)
Overrides:
substring in class AbstractStringBuilder
Parameters:
start

substring

public synchronized String substring(int start,
                                     int end)
Overrides:
substring in class AbstractStringBuilder
Parameters:
start
end

toString

public synchronized String toString()
Overrides:
toString in class AbstractStringBuilder

trimToSize

public synchronized void trimToSize()
Overrides:
trimToSize in class AbstractStringBuilder


This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.
This page displays the Jadeite version of the documention, which is derived from the offical documentation that contains this copyright notice:
Copyright 2008 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
The official Sun™ documentation can be found here at http://java.sun.com/javase/6/docs/api/.