Class java.lang.StringBuffer
All Packages    This Package    Previous    Next

Class java.lang.StringBuffer

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

public 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 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, OutputStreamBuffer
Version:
1.15, 31 Jan 1995
Author:
Arthur van Hoff

StringBuffer()
Constructs an empty string buffer.
StringBuffer(int)
Constructs a string buffer with the specified initial length.
StringBuffer(String)
Constructs a string buffer with the specified initial value.

append(Object)
Appends an object to the end of this buffer.
append(String)
Appends a string to the end of this buffer.
append(char[])
Appends an array of characters to the end of this buffer.
append(char[], int, int)
Appends a part of an array of characters to the end of this buffer.
append(boolean)
Appends a boolean to the end of this buffer.
append(int)
Appends an integer to the end of this buffer.
append(long)
Appends a long to the end of this buffer.
append(float)
Appends a float to the end of this buffer.
append(double)
Appends a double to the end of this buffer.
appendChar(int)
Appends a character to the end of this buffer.
capacity()
Returns the current capacity of the string buffer.
charAt(int)
Returns the character at the specified index.
copyWhenShared()
Copies the buffer value if it is shared.
ensureCapacity(int)
Ensures that the capacity of the buffer is at least equal to the specified minimum.
getChars(int, int, char[], int)
Copies the characters of the specified substring (determined by beginIndex and endIndex) into the character array, starting at the array's dstIndex location.
insert(int, Object)
Inserts an object into the string buffer.
insert(int, String)
Inserts a string into the string buffer.
insert(int, char[])
Inserts an array of characters into the string buffer.
insert(int, boolean)
Inserts a boolean into the string buffer.
insert(int, int)
Inserts an integer into the string buffer.
insert(int, long)
Inserts a long into the string buffer.
insert(int, float)
Inserts a float into the string buffer.
insert(int, double)
Inserts a double into the string buffer.
insertChar(int, char)
Inserts a character into the string buffer.
length()
Returns the length (character count) of the buffer.
setCharAt(int, char)
Sets the character at the index to be newCharacter.
setLength(int)
Sets the length of the string.
toString()
Converts to a String

StringBuffer
  public StringBuffer()
Constructs an empty string buffer.

StringBuffer

  public StringBuffer(int length)
Constructs a 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

length
  public int length()
Returns the length (character count) of the buffer.
Returns:
the number of characters in 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.
Returns:
the capacity of the buffer

copyWhenShared

  public void copyWhenShared()
Copies the buffer value if it is shared.

ensureCapacity

  public synchronized 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 synchronized 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: StringIndexOutOfRangeException
invalid length

charAt

  public synchronized 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
Returns:
the desired character

getChars

  public synchronized void getChars(int srcBegin,
                                    int srcEnd,
                                    char dst[],
                                    int dstBegin)
Copies the characters of the specified substring (determined by beginIndex and endIndex) into the character array, starting at the array's dstIndex location. Both scrBegin and scrEnd 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: StringIndexOutOfRangeException
Invalid index into the buffer

setCharAt

  public synchronized void setCharAt(int index,
                                     char ch)
Sets the character at the index to be newCharacter.
Parameters:
index - the index of the character
ch - the new character
Throws: ArrayIndexOutOfBoundsException
Invalid index

append

  public synchronized 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 synchronized 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 synchronized 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 synchronized 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(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.

appendChar

  public synchronized StringBuffer appendChar(int ch)
Appends a character to the end of this buffer.
Parameters:
ch - the character to be appended
Returns:
the StringBuffer itself, NOT a new one.

insert

  public synchronized 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: ArrayIndexOutOfBoundsException
Invalid offset

insert

  public synchronized 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: ArrayIndexOutOfBoundsException
Invalid offset

insert

  public synchronized 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: ArrayIndexOutOfBoundsException
Invalid offset

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: ArrayIndexOutOfBoundsException
Invalid offset

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: ArrayIndexOutOfBoundsException
Invalid offset

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: ArrayIndexOutOfBoundsException
Invalid offset

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: ArrayIndexOutOfBoundsException
Invalid offset

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: ArrayIndexOutOfBoundsException
invalid offset

insertChar

  public synchronized StringBuffer insertChar(int offset,
                                              char ch)
Inserts a character into the string buffer.
Parameters:
offset - the offset at which to insert
ch - the character to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws: ArrayIndexOutOfBoundsException
Invalid offset

toString

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


All Packages    This Package    Previous    Next