Build 1.0_r1(from source)

java.lang
Class StringBuffer

java.lang.Object
  extended by java.lang.AbstractStringBuilder
      extended by java.lang.StringBuffer
All Implemented Interfaces:
Serializable, Appendable, CharSequence

public final class StringBuffer
extends AbstractStringBuilder
implements Appendable, Serializable, CharSequence

StringBuffer is a variable size contiguous indexable array of characters. The length of the StringBuffer is the number of characters it contains. The capacity of the StringBuffer is the number of characters it can hold.

Characters may be inserted at any position up to the length of the StringBuffer, increasing the length of the StringBuffer. Characters at any position in the StringBuffer may be replaced, which does not affect the StringBuffer length.

The capacity of a StringBuffer may be specified when the StringBuffer is created. If the capacity of the StringBuffer is exceeded, the capacity is increased.

Since:
1.0
See Also:
String, StringBuilder, Serialized Form

Field Summary
 
Fields inherited from class java.lang.AbstractStringBuilder
INITIAL_CAPACITY
 
Constructor Summary
StringBuffer()
          Constructs a new StringBuffer using the default capacity.
StringBuffer(CharSequence cs)
           Constructs a StringBuffer and initializes it with the characters in the CharSequence.
StringBuffer(int capacity)
          Constructs a new StringBuffer using the specified capacity.
StringBuffer(String string)
          Constructs a new StringBuffer containing the characters in the specified string and the default capacity.
 
Method Summary
 StringBuffer append(boolean b)
          Adds the string representation of the specified boolean to the end of this StringBuffer.
 StringBuffer append(char ch)
          Adds the specified character to the end of this StringBuffer.
 StringBuffer append(char[] chars)
          Adds the character array to the end of this StringBuffer.
 StringBuffer append(char[] chars, int start, int length)
          Adds the specified sequence of characters to the end of this StringBuffer.
 StringBuffer append(CharSequence s)
           Appends the CharSequence to this buffer.
 StringBuffer append(CharSequence s, int start, int end)
           Appends the subsequence of the CharSequence to this buffer.
 StringBuffer append(double d)
          Adds the string representation of the specified double to the end of this StringBuffer.
 StringBuffer append(float f)
          Adds the string representation of the specified float to the end of this StringBuffer.
 StringBuffer append(int i)
          Adds the string representation of the specified integer to the end of this StringBuffer.
 StringBuffer append(long l)
          Adds the string representation of the specified long to the end of this StringBuffer.
 StringBuffer append(Object obj)
          Adds the string representation of the specified object to the end of this StringBuffer.
 StringBuffer append(String string)
          Adds the specified string to the end of this StringBuffer.
 StringBuffer append(StringBuffer sb)
          Adds the specified StringBuffer to the end of this StringBuffer.
 StringBuffer appendCodePoint(int codePoint)
           Appends the encoded Unicode code point to this object.
 char charAt(int index)
          Returns the character at the specified offset in this StringBuffer.
 int codePointAt(int index)
           Retrieves the Unicode code point value at the index.
 int codePointBefore(int index)
           Retrieves the Unicode code point value that precedes the index.
 int codePointCount(int beginIndex, int endIndex)
           Calculates the number of Unicode code points between beginIndex and endIndex.
 StringBuffer delete(int start, int end)
          Deletes a range of characters.
 StringBuffer deleteCharAt(int location)
          Deletes a single character
 void ensureCapacity(int min)
          Ensures that this StringBuffer can hold the specified number of characters without growing.
 void getChars(int start, int end, char[] buffer, int idx)
          Copies the specified characters in this StringBuffer to the character array starting at the specified offset in the character array.
 int indexOf(String subString, int start)
          Searches in this StringBuffer for the index of the specified character.
 StringBuffer insert(int index, boolean b)
          Inserts the string representation of the specified boolean at the specified offset in this StringBuffer.
 StringBuffer insert(int index, char ch)
          Inserts the character at the specified offset in this StringBuffer.
 StringBuffer insert(int index, char[] chars)
          Inserts the character array at the specified offset in this StringBuffer.
 StringBuffer insert(int index, char[] chars, int start, int length)
          Inserts the specified sequence of characters at the specified offset in this StringBuffer.
 StringBuffer insert(int index, CharSequence s)
           Inserts the CharSequence into this buffer at the index.
 StringBuffer insert(int index, CharSequence s, int start, int end)
           Inserts the CharSequence into this buffer at the index.
 StringBuffer insert(int index, double d)
          Inserts the string representation of the specified double at the specified offset in this StringBuffer.
 StringBuffer insert(int index, float f)
          Inserts the string representation of the specified float at the specified offset in this StringBuffer.
 StringBuffer insert(int index, int i)
          Inserts the string representation of the specified integer at the specified offset in this StringBuffer.
 StringBuffer insert(int index, long l)
          Inserts the string representation of the specified long at the specified offset in this StringBuffer.
 StringBuffer insert(int index, Object obj)
          Inserts the string representation of the specified object at the specified offset in this StringBuffer.
 StringBuffer insert(int index, String string)
          Inserts the string at the specified offset in this StringBuffer.
 int lastIndexOf(String subString, int start)
          Searches in this StringBuffer for the index of the specified character.
 int offsetByCodePoints(int index, int codePointOffset)
           Returns the index within this object that is offset from index by codePointOffset code points.
 StringBuffer replace(int start, int end, String string)
          Replace a range of characters with the characters in the specified String.
 StringBuffer reverse()
          Reverses the order of characters in this StringBuffer.
 void setCharAt(int index, char ch)
          Sets the character at the specified offset in this StringBuffer.
 void setLength(int length)
          Sets the length of this StringBuffer to the specified length.
 CharSequence subSequence(int start, int end)
          Copies a range of characters into a new String.
 String substring(int start)
          Copies a range of characters into a new String.
 String substring(int start, int end)
          Copies a range of characters into a new String.
 String toString()
          Returns the contents of this StringBuffer.
 void trimToSize()
           Trims the storage capacity of this buffer down to the size of the current character sequence.
 
Methods inherited from class java.lang.AbstractStringBuilder
append0, append0, append0, append0, append0, appendNull, capacity, delete0, deleteCharAt0, getValue, indexOf, insert0, insert0, insert0, insert0, insert0, lastIndexOf, length, replace0, reverse0, set, shareValue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.lang.CharSequence
length
 

Constructor Detail

StringBuffer

public StringBuffer()
Constructs a new StringBuffer using the default capacity.


StringBuffer

public StringBuffer(int capacity)
Constructs a new StringBuffer using the specified capacity.

Parameters:
capacity - the initial capacity

StringBuffer

public StringBuffer(String string)
Constructs a new StringBuffer containing the characters in the specified string and the default capacity.

Parameters:
string - the string content with which to initialize the new StringBuffer instance
Throws:
NullPointerException - on supplying a null value of string

StringBuffer

public StringBuffer(CharSequence cs)

Constructs a StringBuffer and initializes it with the characters in the CharSequence.

Parameters:
cs - The CharSequence to initialize the instance.
Throws:
NullPointerException - if the cs parameter is null.
Since:
1.5
Method Detail

append

public StringBuffer append(boolean b)
Adds the string representation of the specified boolean to the end of this StringBuffer.

Parameters:
b - the boolean
Returns:
this StringBuffer

append

public StringBuffer append(char ch)
Adds the specified character to the end of this StringBuffer.

Specified by:
append in interface Appendable
Parameters:
ch - a character
Returns:
this StringBuffer

append

public StringBuffer append(double d)
Adds the string representation of the specified double to the end of this StringBuffer.

Parameters:
d - the double
Returns:
this StringBuffer

append

public StringBuffer append(float f)
Adds the string representation of the specified float to the end of this StringBuffer.

Parameters:
f - the float
Returns:
this StringBuffer

append

public StringBuffer append(int i)
Adds the string representation of the specified integer to the end of this StringBuffer.

Parameters:
i - the integer
Returns:
this StringBuffer

append

public StringBuffer append(long l)
Adds the string representation of the specified long to the end of this StringBuffer.

Parameters:
l - the long
Returns:
this StringBuffer

append

public StringBuffer append(Object obj)
Adds the string representation of the specified object to the end of this StringBuffer.

Parameters:
obj - the object
Returns:
this StringBuffer

append

public StringBuffer append(String string)
Adds the specified string to the end of this StringBuffer.

Parameters:
string - the string
Returns:
this StringBuffer

append

public StringBuffer append(StringBuffer sb)
Adds the specified StringBuffer to the end of this StringBuffer.

Parameters:
sb - the StringBuffer
Returns:
this StringBuffer
Since:
1.4

append

public StringBuffer append(char[] chars)
Adds the character array to the end of this StringBuffer.

Parameters:
chars - the character array
Returns:
this StringBuffer
Throws:
NullPointerException - when chars is null

append

public StringBuffer append(char[] chars,
                           int start,
                           int length)
Adds the specified sequence of characters to the end of this StringBuffer.

Parameters:
chars - a character array
start - the starting offset
length - the number of characters
Returns:
this StringBuffer
Throws:
ArrayIndexOutOfBoundsException - when length < 0,="" start=""><> or start + length > chars.length
NullPointerException - when chars is null

append

public StringBuffer append(CharSequence s)

Appends the CharSequence to this buffer. If the CharSequence is null, then the string "null" is appended.

Specified by:
append in interface Appendable
Parameters:
s - The CharSequence to append.
Returns:
A reference to this object.
Since:
1.5

append

public StringBuffer append(CharSequence s,
                           int start,
                           int end)

Appends the subsequence of the CharSequence to this buffer. If the CharSequence is null, then the string "null" is used to extract a subsequence.

Specified by:
append in interface Appendable
Parameters:
s - The CharSequence to append.
start - The inclusive start index of the subsequence of the CharSequence.
end - The exclusive end index of the subsequence of the CharSequence.
Returns:
A reference to this object.
Throws:
IndexOutOfBoundsException - if start or end are negative, start is greater than end or end is greater than the length of s.
Since:
1.5

appendCodePoint

public StringBuffer appendCodePoint(int codePoint)

Appends the encoded Unicode code point to this object. The code point is converted to a char[] as defined by Character.toChars(int).

Parameters:
codePoint - The Unicode code point to encode and append.
Returns:
A reference to this object.
Since:
1.5
See Also:
Character.toChars(int)

charAt

public char charAt(int index)
Returns the character at the specified offset in this StringBuffer.

Specified by:
charAt in interface CharSequence
Overrides:
charAt in class AbstractStringBuilder
Parameters:
index - the zero-based index in this StringBuffer
Returns:
the character at the index
Throws:
IndexOutOfBoundsException - when index <> or index >= length()

codePointAt

public int codePointAt(int index)

Retrieves the Unicode code point value at the index.

Overrides:
codePointAt in class AbstractStringBuilder
Parameters:
index - The index to the char code unit within this object.
Returns:
The Unicode code point value.
Throws:
IndexOutOfBoundsException - if index is negative or greater than or equal to AbstractStringBuilder.length().
Since:
1.5
See Also:
Character, Character.codePointAt(char[], int, int)

codePointBefore

public int codePointBefore(int index)

Retrieves the Unicode code point value that precedes the index.

Overrides:
codePointBefore in class AbstractStringBuilder
Parameters:
index - The index to the char code unit within this object.
Returns:
The Unicode code point value.
Throws:
IndexOutOfBoundsException - if index is less than 1 or greater than AbstractStringBuilder.length().
Since:
1.5
See Also:
Character, Character.codePointBefore(char[], int, int)

codePointCount

public int codePointCount(int beginIndex,
                          int endIndex)

Calculates the number of Unicode code points between beginIndex and endIndex.

Overrides:
codePointCount in class AbstractStringBuilder
Parameters:
beginIndex - The inclusive beginning index of the subsequence.
endIndex - The exclusive end index of the subsequence.
Returns:
The number of Unicode code points in the subsequence.
Throws:
IndexOutOfBoundsException - if beginIndex is negative or greater than endIndex or endIndex is greater than AbstractStringBuilder.length().
Since:
1.5

delete

public StringBuffer delete(int start,
                           int end)
Deletes a range of characters.

Parameters:
start - the offset of the first character
end - the offset one past the last character
Returns:
this StringBuffer
Throws:
StringIndexOutOfBoundsException - when start < 0,="" start=""> end or end > length()

deleteCharAt

public StringBuffer deleteCharAt(int location)
Deletes a single character

Parameters:
location - the offset of the character to delete
Returns:
this StringBuffer
Throws:
StringIndexOutOfBoundsException - when location <> or location >= length()

ensureCapacity

public void ensureCapacity(int min)
Ensures that this StringBuffer can hold the specified number of characters without growing.

Overrides:
ensureCapacity in class AbstractStringBuilder
Parameters:
min - the minimum number of elements that this StringBuffer will hold before growing

getChars

public void getChars(int start,
                     int end,
                     char[] buffer,
                     int idx)
Copies the specified characters in this StringBuffer to the character array starting at the specified offset in the character array.

Overrides:
getChars in class AbstractStringBuilder
Parameters:
start - the starting offset of characters to copy
end - the ending offset of characters to copy
buffer - the destination character array
idx - the starting offset in the character array
Throws:
IndexOutOfBoundsException - when start < 0,="" end=""> length(), start > end, index < 0,="" end="" -="" start=""> buffer.length - index
NullPointerException - when buffer is null

indexOf

public int indexOf(String subString,
                   int start)
Searches in this StringBuffer for the index of the specified character. The search for the character starts at the specified offset and moves towards the end.

Overrides:
indexOf in class AbstractStringBuilder
Parameters:
subString - the string to find
start - the starting offset
Returns:
the index in this StringBuffer of the specified character, -1 if the character isn't found
Since:
1.4
See Also:
lastIndexOf(String,int)

insert

public StringBuffer insert(int index,
                           char ch)
Inserts the character at the specified offset in this StringBuffer.

Parameters:
index - the index at which to insert
ch - the character to insert
Returns:
this StringBuffer
Throws:
ArrayIndexOutOfBoundsException - when index <> or index > length()

insert

public StringBuffer insert(int index,
                           boolean b)
Inserts the string representation of the specified boolean at the specified offset in this StringBuffer.

Parameters:
index - the index at which to insert
b - the boolean to insert
Returns:
this StringBuffer
Throws:
StringIndexOutOfBoundsException - when index <> or index > length()

insert

public StringBuffer insert(int index,
                           int i)
Inserts the string representation of the specified integer at the specified offset in this StringBuffer.

Parameters:
index - the index at which to insert
i - the integer to insert
Returns:
this StringBuffer
Throws:
StringIndexOutOfBoundsException - when index <> or index > length()

insert

public StringBuffer insert(int index,
                           long l)
Inserts the string representation of the specified long at the specified offset in this StringBuffer.

Parameters:
index - the index at which to insert
l - the long to insert
Returns:
this StringBuffer
Throws:
StringIndexOutOfBoundsException - when index <> or index > length()

insert

public StringBuffer insert(int index,
                           double d)
Inserts the string representation of the specified double at the specified offset in this StringBuffer.

Parameters:
index - the index at which to insert
d - the double to insert
Returns:
this StringBuffer
Throws:
StringIndexOutOfBoundsException - when index <> or index > length()

insert

public StringBuffer insert(int index,
                           float f)
Inserts the string representation of the specified float at the specified offset in this StringBuffer.

Parameters:
index - the index at which to insert
f - the float to insert
Returns:
this StringBuffer
Throws:
StringIndexOutOfBoundsException - when index <> or index > length()

insert

public StringBuffer insert(int index,
                           Object obj)
Inserts the string representation of the specified object at the specified offset in this StringBuffer.

Parameters:
index - the index at which to insert
obj - the object to insert
Returns:
this StringBuffer
Throws:
StringIndexOutOfBoundsException - when index <> or index > length()

insert

public StringBuffer insert(int index,
                           String string)
Inserts the string at the specified offset in this StringBuffer.

Parameters:
index - the index at which to insert
string - the string to insert
Returns:
this StringBuffer
Throws:
StringIndexOutOfBoundsException - when index <> or index > length()

insert

public StringBuffer insert(int index,
                           char[] chars)
Inserts the character array at the specified offset in this StringBuffer.

Parameters:
index - the index at which to insert
chars - the character array to insert
Returns:
this StringBuffer
Throws:
StringIndexOutOfBoundsException - when index <> or index > length()
NullPointerException - when chars is null

insert

public StringBuffer insert(int index,
                           char[] chars,
                           int start,
                           int length)
Inserts the specified sequence of characters at the specified offset in this StringBuffer.

Parameters:
index - the index at which to insert
chars - a character array
start - the starting offset
length - the number of characters
Returns:
this StringBuffer
Throws:
StringIndexOutOfBoundsException - when length < 0,="" start=""><> start + length > chars.length, index <> or index > length()
NullPointerException - when chars is null

insert

public StringBuffer insert(int index,
                           CharSequence s)

Inserts the CharSequence into this buffer at the index. If CharSequence is null, then the string "null" is inserted.

Parameters:
index - The index of this buffer to insert the sequence.
s - The CharSequence to insert.
Returns:
A reference to this object.
Throws:
IndexOutOfBoundsException - if the index is invalid.
Since:
1.5

insert

public StringBuffer insert(int index,
                           CharSequence s,
                           int start,
                           int end)

Inserts the CharSequence into this buffer at the index. If CharSequence is null, then the string "null" is inserted.

Parameters:
index - The index of this buffer to insert the sequence.
s - The CharSequence to insert.
start - The inclusive start index of the subsequence of the CharSequence.
end - The exclusive end index of the subsequence of the CharSequence.
Returns:
A reference to this object.
Throws:
IndexOutOfBoundsException - if index is negative or greater than the current length, start or end are negative, start is greater than end or end is greater than the length of s.
Since:
1.5

lastIndexOf

public int lastIndexOf(String subString,
                       int start)
Searches in this StringBuffer for the index of the specified character. The search for the character starts at the specified offset and moves towards the beginning.

Overrides:
lastIndexOf in class AbstractStringBuilder
Parameters:
subString - the string to find
start - the starting offset
Returns:
the index in this StringBuffer of the specified character, -1 if the character isn't found
Since:
1.4
See Also:
indexOf(String,int)

offsetByCodePoints

public int offsetByCodePoints(int index,
                              int codePointOffset)

Returns the index within this object that is offset from index by codePointOffset code points.

Overrides:
offsetByCodePoints in class AbstractStringBuilder
Parameters:
index - The index within this object to calculate the offset from.
codePointOffset - The number of code points to count.
Returns:
The index within this object that is the offset.
Throws:
IndexOutOfBoundsException - if index is negative or greater than AbstractStringBuilder.length() or if there aren't enough code points before or after index to match codePointOffset.
Since:
1.5

replace

public StringBuffer replace(int start,
                            int end,
                            String string)
Replace a range of characters with the characters in the specified String.

Parameters:
start - the offset of the first character
end - the offset one past the last character
string - a String
Returns:
this StringBuffer
Throws:
StringIndexOutOfBoundsException - when start <> or start > end

reverse

public StringBuffer reverse()
Reverses the order of characters in this StringBuffer.

Returns:
this StringBuffer

setCharAt

public void setCharAt(int index,
                      char ch)
Sets the character at the specified offset in this StringBuffer.

Overrides:
setCharAt in class AbstractStringBuilder
Parameters:
index - the zero-based index in this StringBuffer
ch - the character
Throws:
IndexOutOfBoundsException - when index <> or index >= length()

setLength

public void setLength(int length)
Sets the length of this StringBuffer to the specified length. If there are more than length characters in this StringBuffer, the characters at end are lost. If there are less than length characters in the StringBuffer, the additional characters are set to \\u0000.

Overrides:
setLength in class AbstractStringBuilder
Parameters:
length - the new length of this StringBuffer
Throws:
IndexOutOfBoundsException - when length <>
See Also:
AbstractStringBuilder.length()

subSequence

public CharSequence subSequence(int start,
                                int end)
Copies a range of characters into a new String.

Specified by:
subSequence in interface CharSequence
Overrides:
subSequence in class AbstractStringBuilder
Parameters:
start - the offset of the first character
end - the offset one past the last character
Returns:
a new String containing the characters from start to end - 1
Throws:
IndexOutOfBoundsException - when start < 0,="" start=""> end or end > length()
Since:
1.4

substring

public String substring(int start)
Copies a range of characters into a new String.

Overrides:
substring in class AbstractStringBuilder
Parameters:
start - the offset of the first character
Returns:
a new String containing the characters from start to the end of the string
Throws:
StringIndexOutOfBoundsException - when start <> or start > length()

substring

public String substring(int start,
                        int end)
Copies a range of characters into a new String.

Overrides:
substring in class AbstractStringBuilder
Parameters:
start - the offset of the first character
end - the offset one past the last character
Returns:
a new String containing the characters from start to end - 1
Throws:
StringIndexOutOfBoundsException - when start < 0,="" start=""> end or end > length()

toString

public String toString()
Returns the contents of this StringBuffer.

Specified by:
toString in interface CharSequence
Overrides:
toString in class AbstractStringBuilder
Returns:
a String containing the characters in this StringBuffer

trimToSize

public void trimToSize()

Trims the storage capacity of this buffer down to the size of the current character sequence. Execution of this method may change the results returned by the AbstractStringBuilder.capacity() method, but this is not required.

Overrides:
trimToSize in class AbstractStringBuilder
Since:
1.5

Build 1.0_r1(from source)

Please submit a feedback, bug or feature