java.lang
Class StringBuffer
java.lang.Object
|
+--java.lang.StringBuffer
All Implemented Interfaces:
Serializable, CharSequence
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
is the same as
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).
Since:Authors:- Paul Fisher
- John Keiser
- Tom Tromey
- Eric Blake <ebb9@email.byu.edu>
See Also:
java.lang.StringBuffer | append(java.lang.Object obj)
|
synchronized java.lang.StringBuffer | append(java.lang.String str)
|
synchronized java.lang.StringBuffer | append(java.lang.StringBuffer stringBuffer)
|
java.lang.StringBuffer | append(char[] data)
|
synchronized java.lang.StringBuffer | append(char[] data, int offset, int count)
|
java.lang.StringBuffer | append(boolean bool)
|
synchronized java.lang.StringBuffer | append(char ch)
|
java.lang.StringBuffer | append(int inum)
|
java.lang.StringBuffer | append(long lnum)
|
java.lang.StringBuffer | append(float fnum)
|
java.lang.StringBuffer | append(double dnum)
|
synchronized int | capacity()
|
synchronized char | charAt(int index)
|
synchronized java.lang.StringBuffer | delete(int start, int end)
|
java.lang.StringBuffer | deleteCharAt(int index)
|
synchronized void | ensureCapacity(int minimumCapacity)
|
synchronized void | getChars(int srcOffset, int srcEnd, char[] dst, int dstOffset)
|
int | indexOf(java.lang.String str)
|
synchronized int | indexOf(java.lang.String str, int fromIndex)
|
synchronized java.lang.StringBuffer | insert(int offset, char[] str, int str_offset, int len)
|
java.lang.StringBuffer | insert(int offset, java.lang.Object obj)
|
synchronized java.lang.StringBuffer | insert(int offset, java.lang.String str)
|
java.lang.StringBuffer | insert(int offset, char[] data)
|
java.lang.StringBuffer | insert(int offset, boolean bool)
|
synchronized java.lang.StringBuffer | insert(int offset, char ch)
|
java.lang.StringBuffer | insert(int offset, int inum)
|
java.lang.StringBuffer | insert(int offset, long lnum)
|
java.lang.StringBuffer | insert(int offset, float fnum)
|
java.lang.StringBuffer | insert(int offset, double dnum)
|
int | lastIndexOf(java.lang.String str)
|
synchronized int | lastIndexOf(java.lang.String str, int fromIndex)
|
synchronized int | length()
|
synchronized java.lang.StringBuffer | replace(int start, int end, java.lang.String str)
|
synchronized java.lang.StringBuffer | reverse()
|
synchronized void | setCharAt(int index, char ch)
|
synchronized void | setLength(int newLength)
|
java.lang.CharSequence | subSequence(int beginIndex, int endIndex)
|
java.lang.String | substring(int beginIndex)
|
synchronized java.lang.String | substring(int beginIndex, int endIndex)
|
java.lang.String | toString()
|
StringBuffer
public StringBuffer()
Create a new StringBuffer with default capacity 16.
StringBuffer
public StringBuffer(int capacity)
Create an empty StringBuffer
with the specified initial
capacity.
Parameters:
Throws:
StringBuffer
public StringBuffer(java.lang.String str)
Create a new StringBuffer
with the characters in the
specified String
. Initial capacity will be the size of the
String plus 16.
Parameters:
Throws:
append
public StringBuffer append(boolean bool)
Append the String
value of the argument to this
StringBuffer
. Uses String.valueOf()
to convert
to String
.
Parameters:
Returns:
See Also:
append
public StringBuffer append(char[] data)
Append the char
array to this StringBuffer
.
This is similar (but more efficient) than
append(new String(data))
, except in the case of null.
Parameters:
Returns:
Throws:
See Also:
append
public synchronized StringBuffer append(char ch)
Append the char
to this StringBuffer
.
Parameters:
Returns:
append
public synchronized StringBuffer append(char[] data, int offset, int count)
Append part of the char
array to this
StringBuffer
. This is similar (but more efficient) than
append(new String(data, offset, count))
, except in the case
of null.
Parameters:
Returns:
Throws:
append
public StringBuffer append(double dnum)
Append the String
value of the argument to this
StringBuffer
. Uses String.valueOf()
to convert
to String
.
Parameters:
Returns:
See Also:
append
public StringBuffer append(float fnum)
Append the String
value of the argument to this
StringBuffer
. Uses String.valueOf()
to convert
to String
.
Parameters:
Returns:
See Also:
append
public StringBuffer append(int inum)
Append the String
value of the argument to this
StringBuffer
. Uses String.valueOf()
to convert
to String
.
Parameters:
Returns:
See Also:
append
public StringBuffer append(java.lang.Object obj)
Append the String
value of the argument to this
StringBuffer
. Uses String.valueOf()
to convert
to String
.
Parameters:
Returns:
See Also:
append
public synchronized StringBuffer append(java.lang.String str)
Append the String
to this StringBuffer
. If
str is null, the String "null" is appended.
Parameters:
Returns:
append
public synchronized StringBuffer append(java.lang.StringBuffer stringBuffer)
Append the StringBuffer
value of the argument to this
StringBuffer
. This behaves the same as
append((Object) stringBuffer)
, except it is more efficient.
Since:Parameters:
Returns:
See Also:
append
public StringBuffer append(long lnum)
Append the String
value of the argument to this
StringBuffer
. Uses String.valueOf()
to convert
to String
.
Parameters:
Returns:
See Also:
capacity
public synchronized int capacity()
Get the total number of characters this StringBuffer
can
support before it must be grown. Not to be confused with length.
Returns:
- the capacity of this
StringBuffer
See Also:
charAt
public synchronized char charAt(int index)
Get the character at the specified index.
Parameters:
Returns:
- the character at the specified index
Throws:
IndexOutOfBoundsException
- if index is negative or >= length()
(while unspecified, this is a StringIndexOutOfBoundsException)
delete
public synchronized StringBuffer delete(int start, int end)
Delete characters from this StringBuffer
.
delete(10, 12)
will delete 10 and 11, but not 12. It is
harmless for end to be larger than length().
Since:Parameters:
Returns:
Throws:
deleteCharAt
public StringBuffer deleteCharAt(int index)
Delete a character from this StringBuffer
.
Since:Parameters:
Returns:
Throws:
ensureCapacity
public synchronized void ensureCapacity(int minimumCapacity)
Increase the capacity of this StringBuffer
. This will
ensure that an expensive growing operation will not occur until
minimumCapacity
is reached. The buffer is grown to the
larger of minimumCapacity
and
capacity() * 2 + 2
, if it is not already large enough.
Parameters:
See Also:
getChars
public synchronized 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:
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)
See Also:
indexOf
public int indexOf(java.lang.String str)
Finds the first instance of a substring in this StringBuffer.
Since:Parameters:
Returns:
- location (base 0) of the String, or -1 if not found
Throws:
See Also:
indexOf
public synchronized int indexOf(java.lang.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.
Since:Parameters:
Returns:
- location (base 0) of the String, or -1 if not found
Throws:
insert
public StringBuffer insert(int offset, boolean bool)
Insert the String
value of the argument into this
StringBuffer
. Uses String.valueOf()
to convert
to String
.
Parameters:
Returns:
Throws:
See Also:
insert
public StringBuffer insert(int offset, char[] data)
Insert the char[]
argument into this
StringBuffer
.
Parameters:
Returns:
Throws:
See Also:
insert
public synchronized StringBuffer insert(int offset, char ch)
Insert the char
argument into this StringBuffer
.
Parameters:
Returns:
Throws:
insert
public synchronized StringBuffer insert(int offset, char[] str, int str_offset, int len)
Insert a subarray of the char[]
argument into this
StringBuffer
.
Since:Parameters:
Returns:
Throws:
insert
public StringBuffer insert(int offset, double dnum)
Insert the String
value of the argument into this
StringBuffer
. Uses String.valueOf()
to convert
to String
.
Parameters:
Returns:
Throws:
See Also:
insert
public StringBuffer insert(int offset, float fnum)
Insert the String
value of the argument into this
StringBuffer
. Uses String.valueOf()
to convert
to String
.
Parameters:
Returns:
Throws:
See Also:
insert
public StringBuffer insert(int offset, int inum)
Insert the String
value of the argument into this
StringBuffer
. Uses String.valueOf()
to convert
to String
.
Parameters:
Returns:
Throws:
See Also:
insert
public StringBuffer insert(int offset, java.lang.Object obj)
Insert the String
value of the argument into this
StringBuffer
. Uses String.valueOf()
to convert
to String
.
Parameters:
Returns:
Throws:
See Also:
insert
public synchronized StringBuffer insert(int offset, java.lang.String str)
Insert the String
argument into this
StringBuffer
. If str is null, the String "null" is used
instead.
Parameters:
Returns:
Throws:
insert
public StringBuffer insert(int offset, long lnum)
Insert the String
value of the argument into this
StringBuffer
. Uses String.valueOf()
to convert
to String
.
Parameters:
Returns:
Throws:
See Also:
lastIndexOf
public int lastIndexOf(java.lang.String str)
Finds the last instance of a substring in this StringBuffer.
Since:Parameters:
Returns:
- location (base 0) of the String, or -1 if not found
Throws:
See Also:
lastIndexOf
public synchronized int lastIndexOf(java.lang.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.
Since:Parameters:
Returns:
- location (base 0) of the String, or -1 if not found
Throws:
length
public synchronized int length()
Get the length of the String
this StringBuffer
would create. Not to be confused with the capacity of the
StringBuffer
.
Returns:
- the length of this
StringBuffer
See Also:
replace
public synchronized StringBuffer replace(int start, int end, java.lang.String str)
Replace characters between index start
(inclusive) and
end
(exclusive) with str
. If end
is larger than the size of this StringBuffer, all characters after
start
are replaced.
Since:Parameters:
Returns:
Throws:
reverse
public synchronized StringBuffer reverse()
Reverse the characters in this StringBuffer. The same sequence of
characters exists, but in the reverse index ordering.
Returns:
setCharAt
public synchronized void setCharAt(int index, char ch)
Set the character at the specified index.
Parameters:
Throws:
IndexOutOfBoundsException
- if index is negative or >= length()
(while unspecified, this is a StringIndexOutOfBoundsException)
setLength
public synchronized 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 first newLength
characters of the old array will be preserved, and the remaining
characters are truncated.
Parameters:
Throws:
See Also:
subSequence
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 as substring(beginIndex, endIndex)
, to satisfy
the CharSequence interface.
Since:Parameters:
Returns:
- new String which is a substring of this StringBuffer
Throws:
See Also:
substring
public String substring(int beginIndex)
Creates a substring of this StringBuffer, starting at a specified index
and ending at the end of this StringBuffer.
Since:Parameters:
Returns:
- new String which is a substring of this StringBuffer
Throws:
See Also:
substring
public synchronized 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.
Since:Parameters:
Returns:
- new String which is a substring of this StringBuffer
Throws:
toString
public String toString()
Convert this StringBuffer
to a String
. 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.
Returns:
- the characters in this StringBuffer
StringBuffer
represents a changeableString
. It provides the operations required to modify theStringBuffer
, 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 theStringBuffer
can have in it before it has to grow (growing the char array is an expensive operation involvingnew
).Incidentally, compilers often implement the String operator "+" by using a
StringBuffer
operation:a + b
is the same as
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).