java.lang
Class StringBuffer

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

public class StringBuffer
extends Object

The StringBuffer class represents strings of characters which can change at runtime. A StringBuffer will grow dynamically as its contents change. Growing the StringBuffer requires allocating an entirely new buffer and copying the old string to the new buffer. This can result in a lot of unused memory so it is recommended that when a StringBuffer is created the maximum length is specified.


Constructor Summary
StringBuffer()
          Allocate a new empty StringBuffer using the default capacity of 64 characters.
StringBuffer(int capacity)
          Allocate a new empty StringBuffer using the specified capacity.
StringBuffer(String value)
          Allocate a new StringBuffer and copy its contents from a String.
 
Method Summary
 StringBuffer append(char c)
          Append a character to the StringBuffer.
 StringBuffer append(char[] str, int length)
          Append an array of characters to a StringBuffer.
 StringBuffer append(int val)
          Append the string value of an integer to the StringBuffer.
 StringBuffer append(String str)
          Append a String to the StringBuffer.
 int capacity()
          Returns the current capacity of the StringBuffer.
 char charAt(int index)
          Retrieve a character from StringBuffer.
 void clear()
          Clear the content of StringBuffer.
 StringBuffer delete(int start, int end)
          Delete a range of characters from the StringBuffer.
 boolean equals(String sb)
          Compare this StringBuffer to a String to see if they contain the identical sequence of characters.
 boolean equals(StringBuffer sb)
          Compare this StringBuffer to another to see if they contain the identical sequence of characters.
 int indexOf(String str)
          Finds the first occurance of the specified String in this StringBuffer.
 StringBuffer insert(int offset, char c)
          Insert a character into a StringBuffer.
 int length()
          Returns the current length of the StringBuffer.
 StringBuffer subString(int start, int end)
          Extract a substring from the StringBuffer into a new StringBuffer.
 void subString(int start, int end, StringBuffer dest)
          Extract a substring from the StringBuffer into a existing StringBuffer.
 String toNewString()
          Convert the StringBuffer to a string by allocating a new String object.
 String toString()
          Convert the StringBuffer to a string.
 
Methods inherited from class java.lang.Object
equals
 

Constructor Detail

StringBuffer

public StringBuffer()
Allocate a new empty StringBuffer using the default capacity of 64 characters.

StringBuffer

public StringBuffer(int capacity)
Allocate a new empty StringBuffer using the specified capacity.
Parameters:
capacity - the capacity of the new StringBuffer.

StringBuffer

public StringBuffer(String value)
Allocate a new StringBuffer and copy its contents from a String.
Parameters:
value - the String to copy into the StringBuffer.
Method Detail

length

public int length()
Returns the current length of the StringBuffer.
Returns:
the length of the StringBuffer in characters.

capacity

public int capacity()
Returns the current capacity of the StringBuffer. The capacity is the maximum number of characters that can be stored in the StringBuffer without needing to allocate any more memory.
Returns:
the capacity of the StringBuffer in characters.

clear

public void clear()
Clear the content of StringBuffer. This is done by setting the length to zero and does not affect the capacity.

delete

public StringBuffer delete(int start,
                           int end)
Delete a range of characters from the StringBuffer.
Parameters:
start - index of the first character to delete.
end - index of the character after the last character to delete.
Returns:
the StringBuffer.

append

public StringBuffer append(String str)
Append a String to the StringBuffer.
Parameters:
str - the String to append.
the - StringBuffer.

append

public StringBuffer append(char c)
Append a character to the StringBuffer.
Parameters:
c - the character to append.
the - StringBuffer.

append

public StringBuffer append(int val)
Append the string value of an integer to the StringBuffer. The conversion to a String uses an internal buffer and so this method will not cause a memory leak.
Parameters:
val - the integer val to append.
the - StringBuffer.

append

public StringBuffer append(char[] str,
                           int length)
Append an array of characters to a StringBuffer.
Parameters:
str - the array to append.
length - the number of characters from str to append.
Returns:
the StringBuffer.

insert

public StringBuffer insert(int offset,
                           char c)
                    throws IndexOutOfBoundsException
Insert a character into a StringBuffer. All subsequent characters are moved to make space.
Parameters:
offset - the index to insert the character at.
c - the character to insert.
Returns:
the StringBuffer.

toString

public String toString()
Convert the StringBuffer to a string. A single buffer is used to convert to a String. Each time this method is used the previously returned String will be overwritten. If you need to keep the String then use the toNewString method.
Returns:
a String representation of the StringBuffer.

toNewString

public String toNewString()
Convert the StringBuffer to a string by allocating a new String object.
Returns:
a String representation of the StringBuffer.

equals

public boolean equals(StringBuffer sb)
Compare this StringBuffer to another to see if they contain the identical sequence of characters. The comparison is case sensitive.
Parameters:
sb - the StringBuffer to compare to.
Returns:
true if sb is identical to this StringBuffer.

equals

public boolean equals(String sb)
Compare this StringBuffer to a String to see if they contain the identical sequence of characters. The comparison is case sensitive.
Parameters:
sb - the String to compare to.
Returns:
true if sb is identical to this StringBuffer.

charAt

public char charAt(int index)
            throws IndexOutOfBoundsException
Retrieve a character from StringBuffer.
Parameters:
index - the index into the StringBuffer of the character to retrieve. Indexes start at 0 and end at length()-1.
Returns:
the character at the specified index.

subString

public StringBuffer subString(int start,
                              int end)
Extract a substring from the StringBuffer into a new StringBuffer.
Parameters:
start - the index of the first character in the substring.
end - the index of the character after the last character in the substring.
Returns:
a newly allocated StringBuffer containing the substring.

subString

public void subString(int start,
                      int end,
                      StringBuffer dest)
Extract a substring from the StringBuffer into a existing StringBuffer.
Parameters:
start - the index of the first character in the substring.
end - the index of the character after the last character in the substring.
dest - the Stringbuffer to place the substring in. Any existing content in dest is overwritten.

indexOf

public int indexOf(String str)
Finds the first occurance of the specified String in this StringBuffer.
Parameters:
str - the String to search for.
Returns:
the index of the first character matching the String or -1 if the String is not found.