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()
 
to concatenate multiple strings together. In the code shown, the compiler generates references to the StringBuffer class to append the objects together.

As with all classes in the java.lang package, you can't reference the StringBuffer class using the full specifier of java.lang.StringBuffer. The java.lang package is implicitly imported. Instead, you should simply access the StringBuffer like this:

 StringBuffer sb = new StringBuffer(s);
 


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 buffer.
 
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.
 StringBuffer delete(int start, int end)
          Removes the characters in a substring of this StringBuffer.
 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.
 int length()
          Returns the length (character count) of the buffer.
 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
equals, getClass, hashCode, notify, 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 buffer.
Parameters:
str - the initial buffer 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 in characters

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

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.

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.
Parameters:
ch - the character to be appended
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.

toString

public String toString()
Converts to a String representing the data in the buffer.
Overrides:
toString in class Object

delete

public StringBuffer delete(int start,
                           int end)
Removes the characters in a substring of this StringBuffer. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the StringBuffer if no such character exists. If start is equal to end, no changes are made. If any paramter goes beyond limits, it is enforced into limits. If start > end, the string is emptied; Returns this StringBuffer.