GNU Classpath (0.95) | |
Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
java.lang.StringBuffer
StringBuffer
represents a changeable String
.
It provides the operations required to modify the
StringBuffer
, including insert, replace, delete, append,
and reverse. It is thread-safe; meaning that all modifications to a buffer
are in synchronized methods.
StringBuffer
s are variable-length in nature, so even if
you initialize them to a certain size, they can still grow larger than
that. Capacity indicates the number of characters the
StringBuffer
can have in it before it has to grow (growing
the char array is an expensive operation involving new
).
Incidentally, compilers often implement the String operator "+"
by using a StringBuffer
operation:a + b
new StringBuffer().append(a).append(b).toString()
.
Classpath's StringBuffer is capable of sharing memory with Strings for
efficiency. This will help when a StringBuffer is converted to a String
and the StringBuffer is not changed after that (quite common when performing
string concatenation).
String
, Serialized FormConstructor Summary | |
| |
| |
| |
|
Method Summary | |
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer | |
StringBuffer | |
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
int |
|
char |
|
int |
|
int |
|
int |
|
StringBuffer |
|
StringBuffer |
|
void |
|
void |
|
int | |
int | |
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer |
|
StringBuffer | |
StringBuffer | |
StringBuffer |
|
int |
|
int |
|
int |
|
int |
|
StringBuffer | |
StringBuffer |
|
void |
|
void |
|
CharSequence |
|
String |
|
String |
|
String |
|
void |
|
Methods inherited from class java.lang.Object | |
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
public StringBuffer(int capacity)
Create an emptyStringBuffer
with the specified initial capacity.
- Parameters:
capacity
- the initial capacity
- Throws:
NegativeArraySizeException
- if capacity is negative
public StringBuffer(CharSequence seq)
Create a newStringBuffer
with the characters in the specifiedCharSequence
. Initial capacity will be the length of the sequence plus 16; if the sequence reports a length less than or equal to 0, then the initial capacity will be 16.
- Parameters:
seq
- the initializingCharSequence
- Throws:
NullPointerException
- if str is null
- Since:
- 1.5
public StringBuffer(String str)
Create a newStringBuffer
with the characters in the specifiedString
. Initial capacity will be the size of the String plus 16.
- Parameters:
str
- theString
to convert
- Throws:
NullPointerException
- if str is null
public StringBuffer append(boolean bool)
Append theString
value of the argument to thisStringBuffer
. UsesString.valueOf()
to convert toString
.
- Parameters:
bool
- theboolean
to convert and append
- Returns:
- this
StringBuffer
- See Also:
String.valueOf(boolean)
public StringBuffer append(char ch)
Append thechar
to thisStringBuffer
.
- Specified by:
- append in interface Appendable
- Parameters:
ch
- thechar
to append
- Returns:
- this
StringBuffer
public StringBuffer append(char[] data)
Append thechar
array to thisStringBuffer
. This is similar (but more efficient) thanappend(new String(data))
, except in the case of null.
- Parameters:
data
- thechar[]
to append
- Returns:
- this
StringBuffer
- Throws:
NullPointerException
- ifstr
isnull
- See Also:
append(char[],int,int)
public StringBuffer append(char[] data, int offset, int count)
Append part of thechar
array to thisStringBuffer
. This is similar (but more efficient) thanappend(new String(data, offset, count))
, except in the case of null.
- Parameters:
data
- thechar[]
to appendoffset
- the start location instr
count
- the number of characters to get fromstr
- Returns:
- this
StringBuffer
- Throws:
NullPointerException
- ifstr
isnull
IndexOutOfBoundsException
- if offset or count is out of range (while unspecified, this is a StringIndexOutOfBoundsException)
public StringBuffer append(double dnum)
Append theString
value of the argument to thisStringBuffer
. UsesString.valueOf()
to convert toString
.
- Parameters:
dnum
- thedouble
to convert and append
- Returns:
- this
StringBuffer
- See Also:
String.valueOf(double)
public StringBuffer append(float fnum)
Append theString
value of the argument to thisStringBuffer
. UsesString.valueOf()
to convert toString
.
- Parameters:
fnum
- thefloat
to convert and append
- Returns:
- this
StringBuffer
- See Also:
String.valueOf(float)
public StringBuffer append(int inum)
Append theString
value of the argument to thisStringBuffer
. UsesString.valueOf()
to convert toString
.
- Parameters:
inum
- theint
to convert and append
- Returns:
- this
StringBuffer
- See Also:
String.valueOf(int)
public StringBuffer append(CharSequence seq)
Append the characters in theCharSequence
to this buffer.
- Specified by:
- append in interface Appendable
- Parameters:
seq
- theCharSequence
providing the characters
- Returns:
- this
StringBuffer
- Since:
- 1.5
public StringBuffer append(CharSequence seq, int start, int end)
Append some characters from theCharSequence
to this buffer. If the argument is null, the four characters "null" are appended.
- Specified by:
- append in interface Appendable
- Parameters:
seq
- theCharSequence
providing the charactersstart
- the starting indexend
- one past the final index
- Returns:
- this
StringBuffer
- Since:
- 1.5
public StringBuffer append(Object obj)
Append theString
value of the argument to thisStringBuffer
. UsesString.valueOf()
to convert toString
.
- Parameters:
obj
- theObject
to convert and append
- Returns:
- this
StringBuffer
- See Also:
String.valueOf(Object)
,append(String)
public StringBuffer append(String str)
Append theString
to thisStringBuffer
. If str is null, the String "null" is appended.
- Parameters:
str
- theString
to append
- Returns:
- this
StringBuffer
public StringBuffer append(StringBuffer stringBuffer)
Append theStringBuffer
value of the argument to thisStringBuffer
. This behaves the same asappend((Object) stringBuffer)
, except it is more efficient.
- Parameters:
stringBuffer
- theStringBuffer
to convert and append
- Returns:
- this
StringBuffer
- Since:
- 1.4
- See Also:
append(Object)
public StringBuffer append(long lnum)
Append theString
value of the argument to thisStringBuffer
. UsesString.valueOf()
to convert toString
.
- Parameters:
lnum
- thelong
to convert and append
- Returns:
- this
StringBuffer
- See Also:
String.valueOf(long)
public StringBuffer appendCodePoint(int code)
Append the code point to thisStringBuffer
. This is like #append(char), but will append two characters if a supplementary code point is given.
- Parameters:
code
- the code point to append
- Returns:
- this
StringBuffer
- Since:
- 1.5
- See Also:
Character.toChars(int,char[],int)
public int capacity()
Get the total number of characters thisStringBuffer
can support before it must be grown. Not to be confused with length.
- Returns:
- the capacity of this
StringBuffer
- See Also:
length()
,ensureCapacity(int)
public char charAt(int index)
Get the character at the specified index.
- Specified by:
- charAt in interface CharSequence
- Parameters:
index
- the index of the character to get, starting at 0
- Returns:
- the character at the specified index
- Throws:
IndexOutOfBoundsException
- if index is negative or >= length()
public int codePointAt(int index)
Get the code point at the specified index. This is like #charAt(int), but if the character is the start of a surrogate pair, and the following character completes the pair, then the corresponding supplementary code point is returned.
- Parameters:
index
- the index of the codepoint to get, starting at 0
- Returns:
- the codepoint at the specified index
- Throws:
IndexOutOfBoundsException
- if index is negative or >= length()
- Since:
- 1.5
public int codePointBefore(int index)
Get the code point before the specified index. This is like #codePointAt(int), but checks the characters atindex-1
andindex-2
to see if they form a supplementary code point.
- Parameters:
index
- the index just past the codepoint to get, starting at 0
- Returns:
- the codepoint at the specified index
- Throws:
IndexOutOfBoundsException
- if index is negative or >= length()
- Since:
- 1.5
public int codePointCount(int start, int end)
Return the number of code points between two indices in theStringBuffer
. An unpaired surrogate counts as a code point for this purpose. Characters outside the indicated range are not examined, even if the range ends in the middle of a surrogate pair.
- Parameters:
start
- the starting indexend
- one past the ending index
- Returns:
- the number of code points
- Since:
- 1.5
public StringBuffer delete(int start, int end)
Delete characters from thisStringBuffer
.delete(10, 12)
will delete 10 and 11, but not 12. It is harmless for end to be larger than length().
- Parameters:
start
- the first character to deleteend
- the index after the last character to delete
- Returns:
- this
StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if start or end are out of bounds
- Since:
- 1.2
public StringBuffer deleteCharAt(int index)
Delete a character from thisStringBuffer
.
- Parameters:
index
- the index of the character to delete
- Returns:
- this
StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if index is out of bounds
- Since:
- 1.2
public void ensureCapacity(int minimumCapacity)
Increase the capacity of thisStringBuffer
. This will ensure that an expensive growing operation will not occur untilminimumCapacity
is reached. The buffer is grown to the larger ofminimumCapacity
andcapacity() * 2 + 2
, if it is not already large enough.
- Parameters:
minimumCapacity
- the new capacity
- See Also:
capacity()
public void getChars(int srcOffset, int srcEnd, char[] dst, int dstOffset)
Get the specified array of characters.srcOffset - srcEnd
characters will be copied into the array you pass in.
- Parameters:
srcOffset
- the index to start copying from (inclusive)srcEnd
- the index to stop copying from (exclusive)dst
- the array to copy intodstOffset
- the index to start copying into
- Throws:
NullPointerException
- if dst is nullIndexOutOfBoundsException
- if any source or target indices are out of range (while unspecified, source problems cause a StringIndexOutOfBoundsException, and dest problems cause an ArrayIndexOutOfBoundsException)
public int indexOf(String str)
Finds the first instance of a substring in this StringBuffer.
- Parameters:
str
- String to find
- Returns:
- location (base 0) of the String, or -1 if not found
- Throws:
NullPointerException
- if str is null
- Since:
- 1.4
- See Also:
indexOf(String,int)
public int indexOf(String str, int fromIndex)
Finds the first instance of a String in this StringBuffer, starting at a given index. If starting index is less than 0, the search starts at the beginning of this String. If the starting index is greater than the length of this String, or the substring is not found, -1 is returned.
- Parameters:
str
- String to findfromIndex
- index to start the search
- Returns:
- location (base 0) of the String, or -1 if not found
- Throws:
NullPointerException
- if str is null
- Since:
- 1.4
public StringBuffer insert(int offset, boolean bool)
Insert theString
value of the argument into thisStringBuffer
. UsesString.valueOf()
to convert toString
.
- Parameters:
offset
- the place to insert in this bufferbool
- theboolean
to convert and insert
- Returns:
- this
StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if offset is out of bounds
- See Also:
String.valueOf(boolean)
public StringBuffer insert(int offset, char ch)
Insert thechar
argument into thisStringBuffer
.
- Parameters:
offset
- the place to insert in this bufferch
- thechar
to insert
- Returns:
- this
StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if offset is out of bounds
public StringBuffer insert(int offset, char[] data)
Insert thechar[]
argument into thisStringBuffer
.
- Parameters:
offset
- the place to insert in this bufferdata
- thechar[]
to insert
- Returns:
- this
StringBuffer
- Throws:
NullPointerException
- ifdata
isnull
StringIndexOutOfBoundsException
- if offset is out of bounds
- See Also:
insert(int,char[],int,int)
public StringBuffer insert(int offset, char[] str, int str_offset, int len)
Insert a subarray of thechar[]
argument into thisStringBuffer
.
- Parameters:
offset
- the place to insert in this bufferstr
- thechar[]
to insertstr_offset
- the index instr
to start inserting fromlen
- the number of characters to insert
- Returns:
- this
StringBuffer
- Throws:
NullPointerException
- ifstr
isnull
StringIndexOutOfBoundsException
- if any index is out of bounds
- Since:
- 1.2
public StringBuffer insert(int offset, double dnum)
Insert theString
value of the argument into thisStringBuffer
. UsesString.valueOf()
to convert toString
.
- Parameters:
offset
- the place to insert in this bufferdnum
- thedouble
to convert and insert
- Returns:
- this
StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if offset is out of bounds
- See Also:
String.valueOf(double)
public StringBuffer insert(int offset, float fnum)
Insert theString
value of the argument into thisStringBuffer
. UsesString.valueOf()
to convert toString
.
- Parameters:
offset
- the place to insert in this bufferfnum
- thefloat
to convert and insert
- Returns:
- this
StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if offset is out of bounds
- See Also:
String.valueOf(float)
public StringBuffer insert(int offset, int inum)
Insert theString
value of the argument into thisStringBuffer
. UsesString.valueOf()
to convert toString
.
- Parameters:
offset
- the place to insert in this bufferinum
- theint
to convert and insert
- Returns:
- this
StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if offset is out of bounds
- See Also:
String.valueOf(int)
public StringBuffer insert(int offset, CharSequence sequence)
Insert theCharSequence
argument into thisStringBuffer
. If the sequence is null, the String "null" is used instead.
- Parameters:
offset
- the place to insert in this buffersequence
- theCharSequence
to insert
- Returns:
- this
StringBuffer
- Throws:
IndexOutOfBoundsException
- if offset is out of bounds
- Since:
- 1.5
public StringBuffer insert(int offset, CharSequence sequence, int start, int end)
Insert a subsequence of theCharSequence
argument into thisStringBuffer
. If the sequence is null, the String "null" is used instead.
- Parameters:
offset
- the place to insert in this buffersequence
- theCharSequence
to insertstart
- the starting index of the subsequenceend
- one past the ending index of the subsequence
- Returns:
- this
StringBuffer
- Throws:
IndexOutOfBoundsException
- if offset, start, or end are out of bounds
- Since:
- 1.5
public StringBuffer insert(int offset, Object obj)
Insert theString
value of the argument into thisStringBuffer
. UsesString.valueOf()
to convert toString
.
- Parameters:
offset
- the place to insert in this bufferobj
- theObject
to convert and insert
- Returns:
- this
StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if offset is out of bounds
- See Also:
String.valueOf(Object)
public StringBuffer insert(int offset, String str)
Insert theString
argument into thisStringBuffer
. If str is null, the String "null" is used instead.
- Parameters:
offset
- the place to insert in this bufferstr
- theString
to insert
- Returns:
- this
StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if offset is out of bounds
public StringBuffer insert(int offset, long lnum)
Insert theString
value of the argument into thisStringBuffer
. UsesString.valueOf()
to convert toString
.
- Parameters:
offset
- the place to insert in this bufferlnum
- thelong
to convert and insert
- Returns:
- this
StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if offset is out of bounds
- See Also:
String.valueOf(long)
public int lastIndexOf(String str)
Finds the last instance of a substring in this StringBuffer.
- Parameters:
str
- String to find
- Returns:
- location (base 0) of the String, or -1 if not found
- Throws:
NullPointerException
- if str is null
- Since:
- 1.4
- See Also:
lastIndexOf(String,int)
public int lastIndexOf(String str, int fromIndex)
Finds the last instance of a String in this StringBuffer, starting at a given index. If starting index is greater than the maximum valid index, then the search begins at the end of this String. If the starting index is less than zero, or the substring is not found, -1 is returned.
- Parameters:
str
- String to findfromIndex
- index to start the search
- Returns:
- location (base 0) of the String, or -1 if not found
- Throws:
NullPointerException
- if str is null
- Since:
- 1.4
public int length()
Get the length of theString
thisStringBuffer
would create. Not to be confused with the capacity of theStringBuffer
.
- Specified by:
- length in interface CharSequence
- Returns:
- the length of this
StringBuffer
- See Also:
capacity()
,setLength(int)
public int offsetByCodePoints(int start, int codePoints)
Starting at the given index, this counts forward by the indicated number of code points, and then returns the resulting index. An unpaired surrogate counts as a single code point for this purpose.
- Parameters:
start
- the starting indexcodePoints
- the number of code points
- Returns:
- the resulting index
- Since:
- 1.5
public StringBuffer replace(int start, int end, String str)
Replace characters between indexstart
(inclusive) andend
(exclusive) withstr
. Ifend
is larger than the size of this StringBuffer, all characters afterstart
are replaced.
- Parameters:
start
- the beginning index of characters to delete (inclusive)end
- the ending index of characters to delete (exclusive)str
- the newString
to insert
- Returns:
- this
StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if start or end are out of boundsNullPointerException
- if str is null
- Since:
- 1.2
public StringBuffer reverse()
Reverse the characters in this StringBuffer. The same sequence of characters exists, but in the reverse index ordering.
- Returns:
- this
StringBuffer
public void setCharAt(int index, char ch)
Set the character at the specified index.
- Parameters:
index
- the index of the character to set starting at 0ch
- the value to set that character to
- Throws:
IndexOutOfBoundsException
- if index is negative or >= length() (while unspecified, this is a StringIndexOutOfBoundsException)
public void setLength(int newLength)
Set the length of this StringBuffer. If the new length is greater than the current length, all the new characters are set to '\0'. If the new length is less than the current length, the firstnewLength
characters of the old array will be preserved, and the remaining characters are truncated.
- Parameters:
newLength
- the new length
- Throws:
IndexOutOfBoundsException
- if the new length is negative (while unspecified, this is a StringIndexOutOfBoundsException)
- See Also:
length()
public CharSequence subSequence(int beginIndex, int endIndex)
Creates a substring of this StringBuffer, starting at a specified index and ending at one character before a specified index. This is implemented the same assubstring(beginIndex, endIndex)
, to satisfy the CharSequence interface.
- Specified by:
- subSequence in interface CharSequence
- Parameters:
beginIndex
- index to start at (inclusive, base 0)endIndex
- index to end at (exclusive)
- Returns:
- new String which is a substring of this StringBuffer
- Throws:
IndexOutOfBoundsException
- if beginIndex or endIndex is out of bounds
- Since:
- 1.4
- See Also:
substring(int,int)
public String substring(int beginIndex)
Creates a substring of this StringBuffer, starting at a specified index and ending at the end of this StringBuffer.
- Parameters:
beginIndex
- index to start substring (base 0)
- Returns:
- new String which is a substring of this StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if beginIndex is out of bounds
- Since:
- 1.2
- See Also:
substring(int,int)
public String substring(int beginIndex, int endIndex)
Creates a substring of this StringBuffer, starting at a specified index and ending at one character before a specified index.
- Parameters:
beginIndex
- index to start at (inclusive, base 0)endIndex
- index to end at (exclusive)
- Returns:
- new String which is a substring of this StringBuffer
- Throws:
StringIndexOutOfBoundsException
- if beginIndex or endIndex is out of bounds
- Since:
- 1.2
public String toString()
Convert thisStringBuffer
to aString
. The String is composed of the characters currently in this StringBuffer. Note that the result is a copy, and that future modifications to this buffer do not affect the String.
- Specified by:
- toString in interface CharSequence
- Returns:
- the characters in this StringBuffer
public void trimToSize()
This may reduce the amount of memory used by the StringBuffer, by resizing the internal array to remove unused space. However, this method is not required to resize, so this behavior cannot be relied upon.
- Since:
- 1.5
GNU Classpath (0.95) |