This
class represents a mutable string of characters that can grow or
shrink as necessary. Its mutability makes it suitable for processing
text in place, which is not possible with the immutable
String class. Its resizability and the various
methods it implements make it easier to use than a char[
]. Create a StringBuffer with the
StringBuffer( ) constructor. You may pass a
String that contains the initial text for the
buffer to this constructor, but if you do not, the buffer will start
out empty. You may also specify the initial capacity for the buffer
if you can estimate the number of characters the buffer will
eventually hold.
The methods of this class are synchronized, which
makes StringBuffer objects suitable for use by
multiple threads. In Java 5.0 and later, when working with a single
thread, StringBuilder is preferred over this class
because it does not have the overhead of synchronized methods.
StringBuilder implements the same methods as
StringBuffer and can be used in the same way.
Query the character stored at a given index with
charAt( ) and set or
delete that character with
setCharAt(
) or deleteCharAt( ). Use
length( ) to return the length of the buffer, and
use setLength( ) to set the length of the buffer,
truncating it or filling it with null characters
('\u0000') as necessary.
capacity( ) returns the number of characters a
StringBuffer can hold before its internal buffer
needs to be reallocated. If you expect a
StringBuffer to grow substantially and can
approximate its eventual size, you can use ensureCapacity(
) to preallocate sufficient internal storage.
Use the various append( )
methods to append text to the end of the buffer. Use insert(
) to insert text at a specified position within the buffer.
Note that in addition to strings, primitive values, character arrays,
and arbitrary objects may be passed to append( )
and insert( ). These
values are converted to strings before they are appended or inserted.
Use delete( ) to delete a range of characters from
the buffer and use replace( ) to replace a range
of characters with a specified String.
Use substring( )
to convert a portion of a
StringBuffer to a String. The
two versions of this method work just like the same-named methods of
String. Call toString( ) to
obtain the contents of a StringBuffer as a
String object. Or use getChars(
) to
extract the specified range of characters from the
StringBuffer and store them into the specified
character array starting at the specified index of that array.
As of Java 1.4, StringBuffer implements
CharSequence and so also defines a
subSequence( )
method that is like substring( ) but returns its
value as a CharSequence. Java 1.4 also added
indexOf( )
and lastIndexOf( )
methods that search forward or backward (from the optionally
specified index) in a StringBuffer for a sequence
of characters that matches the specified String.
These methods return the index of the matching string or
-1 if no match was found. See also the similarly
named methods of String after which these methods
are modeled.
In
Java 5.0, this class has a new
constructor and new methods for working with
CharSequence objects. It implements the
Appendable interface for use with
java.util.Formatter and includes new methods for
working with 21-bit Unicode characters as int
codepoints.
String concatenation in Java is
performed with the + operator and is implemented,
prior to Java 5.0, using the append( ) method of a
StringBuffer. In Java 5.0 and later,
StringBuilder is used instead. After a string is
processed in a StringBuffer object, it can be
efficiently converted to a String object for
subsequent use. The StringBuffer.toString( )
method is typically implemented so that it does not copy the internal
array of characters. Instead, it shares that array with the new
String object, making a new copy for itself only
if and when further modifications are made to the
StringBuffer object.
public final class
StringBuffer extends AbstractStringBuilder implements CharSequence,
Serializable {
// Public Constructors
public
StringBuffer ( );
public
StringBuffer (String
str );
public
StringBuffer (int
capacity );
5.0 public
StringBuffer (CharSequence
seq );
// Public Instance Methods
public StringBuffer
append (String
str ); synchronized
1.4 public StringBuffer
append (StringBuffer
sb ); synchronized
5.0 public StringBuffer
append (CharSequence
s );
public StringBuffer
append (Object
obj ); synchronized
public StringBuffer
append (char[ ]
str ); synchronized
public StringBuffer
append (long
lng ); synchronized
public StringBuffer
append (float
f ); synchronized
public StringBuffer
append (double
d ); synchronized
public StringBuffer
append (boolean
b ); synchronized
public StringBuffer
append (char
c ); synchronized
public StringBuffer
append (int
i ); synchronized
public StringBuffer
append (char[ ]
str , int
offset , int
len ); synchronized
5.0 public StringBuffer
append (CharSequence
s , int
start , int
end ); synchronized
5.0 public StringBuffer
appendCodePoint (int
codePoint ); synchronized
public char
charAt (int
index ); Implements:CharSequence synchronized
1.2 public StringBuffer
delete (int
start , int
end ); synchronized
1.2 public StringBuffer
deleteCharAt (int
index ); synchronized
public StringBuffer
insert (int
offset , char
c ); synchronized
public StringBuffer
insert (int
offset , boolean
b );
public StringBuffer
insert (int
offset , long
l );
public StringBuffer
insert (int
offset , int
i );
public StringBuffer
insert (int
offset , String
str ); synchronized
public StringBuffer
insert (int
offset , Object
obj ); synchronized
5.0 public StringBuffer
insert (int
dstOffset , CharSequence
s );
public StringBuffer
insert (int
offset , char[ ]
str ); synchronized
public StringBuffer
insert (int
offset , double
d );
public StringBuffer
insert (int
offset , float
f );
1.2 public StringBuffer
insert (int
index , char[ ]
str , int
offset , int
len ); synchronized
5.0 public StringBuffer
insert (int
dstOffset , CharSequence
s , int
start ,
int
end ); synchronized
public int
length ( ); Implements:CharSequence synchronized
1.2 public StringBuffer
replace (int
start , int
end , String
str ); synchronized
public StringBuffer
reverse ( ); synchronized
public String
toString ( ); Implements:CharSequence synchronized
// Methods Implementing CharSequence
public char
charAt (int
index ); synchronized
public int
length ( ); synchronized
1.4 public CharSequence
subSequence (int
start , int
end ); synchronized
public String
toString ( ); synchronized
// Public Methods Overriding AbstractStringBuilder
public int
capacity ( ); synchronized
5.0 public int
codePointAt (int
index ); synchronized
5.0 public int
codePointBefore (int
index ); synchronized
5.0 public int
codePointCount (int
beginIndex , int
endIndex ); synchronized
public void
ensureCapacity (int
minimumCapacity ); synchronized
public void
getChars (int
srcBegin ,
int
srcEnd , char[ ]
dst , int
dstBegin ); synchronized
1.4 public int
indexOf (String
str );
1.4 public int
indexOf (String
str , int
fromIndex ); synchronized
1.4 public int
lastIndexOf (String
str );
1.4 public int
lastIndexOf (String
str , int
fromIndex ); synchronized
5.0 public int
offsetByCodePoints (int
index , int
codePointOffset ); synchronized
public void
setCharAt (int
index , char
ch ); synchronized
public void
setLength (int
newLength ); synchronized
1.2 public String
substring (int
start ); synchronized
1.2 public String
substring (int
start , int
end ); synchronized
5.0 public void
trimToSize ( ); synchronized
}
Too many methods to list.
Too many methods to list.