java.lang
Class StringBuffer

java.lang.Object
  |
  +--java.lang.StringBuffer

public final class StringBuffer
extends Object

This Class is a growable buffer for characters. It is mainly used to create Strings. The compiler uses it to implement the "+" operator. For example:

	"a" + 4 + "c"
 
is compiled to:
	new StringBuffer().append("a").append(4).append("c").toString()
 
Note that the method toString() does not create a copy of the internal buffer. Instead the buffer is marked as shared. Any further changes to the buffer will cause a copy to be made.

See Also:
String, ByteArrayOutputStream

Constructor Summary
StringBuffer()
          Constructs an empty String buffer.
StringBuffer(int length)
          Constructs an empty String buffer with the specified initial length.
StringBuffer(String str)
          Constructs a String buffer with the specified initial value.
 
Method Summary
 StringBuffer append(boolean b)
          Appends a boolean to the end of this buffer.
 StringBuffer append(char c)
          Appends a character to the end of this buffer.
 StringBuffer append(char[] str)
          Appends an array of characters to the end of this buffer.
 StringBuffer append(char[] str, int offset, int len)
          Appends a part of an array of characters to the end of this buffer.
 StringBuffer append(double d)
          Appends a double to the end of this buffer.
 StringBuffer append(float f)
          Appends a float to the end of this buffer.
 StringBuffer append(int i)
          Appends an integer to the end of this buffer.
 StringBuffer append(long l)
          Appends a long to the end of this buffer.
 StringBuffer append(Object obj)
          Appends an object to the end of this buffer.
 StringBuffer append(String str)
          Appends a String to the end of this buffer.
 int capacity()
          Returns the current capacity of the String buffer.
 char charAt(int index)
          Returns the character at the specified index.
 void ensureCapacity(int minimumCapacity)
          Ensures that the capacity of the buffer is at least equal to the specified minimum.
 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
          Copies the characters of the specified substring (determined by srcBegin and srcEnd) into the character array, starting at the array's dstBegin location.
 StringBuffer insert(int offset, boolean b)
          Inserts a boolean into the String buffer.
 StringBuffer insert(int offset, char c)
          Inserts a character into the String buffer.
 StringBuffer insert(int offset, char[] str)
          Inserts an array of characters into the String buffer.
 StringBuffer insert(int offset, double d)
          Inserts a double into the String buffer.
 StringBuffer insert(int offset, float f)
          Inserts a float into the String buffer.
 StringBuffer insert(int offset, int i)
          Inserts an integer into the String buffer.
 StringBuffer insert(int offset, long l)
          Inserts a long into the String buffer.
 StringBuffer insert(int offset, Object obj)
          Inserts an object into the String buffer.
 StringBuffer insert(int offset, String str)
          Inserts a String into the String buffer.
 int length()
          Returns the length (character count) of the buffer.
 StringBuffer reverse()
          Reverse the order of the characters in the String buffer.
 void setCharAt(int index, char ch)
          Changes the character at the specified index to be ch.
 void setLength(int newLength)
          Sets the length of the String.
 String toString()
          Converts to a String representing the data in the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StringBuffer

public StringBuffer()
Constructs an empty String buffer.


StringBuffer

public StringBuffer(int length)
Constructs an empty String buffer with the specified initial length.

Parameters:
length - the initial length

StringBuffer

public StringBuffer(String str)
Constructs a String buffer with the specified initial value.

Parameters:
str - the initial value of the buffer
Method Detail

length

public int length()
Returns the length (character count) of the buffer.


capacity

public int capacity()
Returns the current capacity of the String buffer. The capacity is the amount of storage available for newly inserted characters; beyond which an allocation will occur.


ensureCapacity

public void ensureCapacity(int minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum.

Parameters:
minimumCapacity - the minimum desired capacity

setLength

public void setLength(int newLength)
Sets the length of the String. If the length is reduced, characters are lost. If the length is extended, the values of the new characters are set to 0.

Parameters:
newLength - the new length of the buffer
Throws:
StringIndexOutOfBoundsException - If the length is invalid.

charAt

public char charAt(int index)
Returns the character at the specified index. An index ranges from 0..length()-1.

Parameters:
index - the index of the desired character
Throws:
StringIndexOutOfBoundsException - If the index is invalid.

getChars

public void getChars(int srcBegin,
                     int srcEnd,
                     char[] dst,
                     int dstBegin)
Copies the characters of the specified substring (determined by srcBegin and srcEnd) into the character array, starting at the array's dstBegin location. Both srcBegin and srcEnd must be legal indexes into the buffer.

Parameters:
srcBegin - begin copy at this offset in the String
srcEnd - stop copying at this offset in the String
dst - the array to copy the data into
dstBegin - offset into dst
Throws:
StringIndexOutOfBoundsException - If there is an invalid index into the buffer.

setCharAt

public void setCharAt(int index,
                      char ch)
Changes the character at the specified index to be ch.

Parameters:
index - the index of the character
ch - the new character
Throws:
StringIndexOutOfBoundsException - If the index is invalid.

append

public StringBuffer append(Object obj)
Appends an object to the end of this buffer.

Parameters:
obj - the object to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringBuffer append(String str)
Appends a String to the end of this buffer.

Parameters:
str - the String to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringBuffer append(char[] str)
Appends an array of characters to the end of this buffer.

Parameters:
str - the characters to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringBuffer append(char[] str,
                           int offset,
                           int len)
Appends a part of an array of characters to the end of this buffer.

Parameters:
str - the characters to be appended
offset - where to start
len - the number of characters to add
Returns:
the StringBuffer itself, NOT a new one.

append

public StringBuffer append(boolean b)
Appends a boolean to the end of this buffer.

Parameters:
b - the boolean to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringBuffer append(char c)
Appends a character to the end of this buffer.

Returns:
the StringBuffer itself, NOT a new one.

append

public StringBuffer append(int i)
Appends an integer to the end of this buffer.

Parameters:
i - the integer to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringBuffer append(long l)
Appends a long to the end of this buffer.

Parameters:
l - the long to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringBuffer append(float f)
Appends a float to the end of this buffer.

Parameters:
f - the float to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringBuffer append(double d)
Appends a double to the end of this buffer.

Parameters:
d - the double to be appended
Returns:
the StringBuffer itself, NOT a new one.

insert

public StringBuffer insert(int offset,
                           Object obj)
Inserts an object into the String buffer.

Parameters:
offset - the offset at which to insert
obj - the object to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringBuffer insert(int offset,
                           String str)
Inserts a String into the String buffer.

Parameters:
offset - the offset at which to insert
str - the String to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringBuffer insert(int offset,
                           char[] str)
Inserts an array of characters into the String buffer.

Parameters:
offset - the offset at which to insert
str - the characters to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringBuffer insert(int offset,
                           boolean b)
Inserts a boolean into the String buffer.

Parameters:
offset - the offset at which to insert
b - the boolean to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringBuffer insert(int offset,
                           char c)
Inserts a character into the String buffer.

Parameters:
offset - the offset at which to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset invalid.

insert

public StringBuffer insert(int offset,
                           int i)
Inserts an integer into the String buffer.

Parameters:
offset - the offset at which to insert
i - the integer to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringBuffer insert(int offset,
                           long l)
Inserts a long into the String buffer.

Parameters:
offset - the offset at which to insert
l - the long to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringBuffer insert(int offset,
                           float f)
Inserts a float into the String buffer.

Parameters:
offset - the offset at which to insert
f - the float to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringBuffer insert(int offset,
                           double d)
Inserts a double into the String buffer.

Parameters:
offset - the offset at which to insert
d - the double to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

reverse

public StringBuffer reverse()
Reverse the order of the characters in the String buffer.


toString

public String toString()
Converts to a String representing the data in the buffer.

Overrides:
toString in class Object