StringBuffer
class StringBuffer : Appendable, CharSequence, Comparable<StringBuffer!>, Serializable
A thread-safe, mutable sequence of characters. A string buffer is like a String
, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.
The principal operations on a StringBuffer
are the append
and insert
methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append
method always adds these characters at the end of the buffer; the insert
method adds the characters at a specified point.
For example, if z
refers to a string buffer object whose current contents are "start"
, then the method call z.append("le")
would cause the string buffer to contain "startle"
, whereas z.insert(4, "le")
would alter the string buffer to contain "starlet"
.
In general, if sb refers to an instance of a StringBuffer
, then sb.append(x)
has the same effect as sb.insert(sb.length(), x)
.
Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence), this class synchronizes only on the string buffer performing the operation, not on the source. Note that while StringBuffer
is designed to be safe to use concurrently from multiple threads, if the constructor or the append
or insert
operation is passed a source sequence that is shared across threads, the calling code must ensure that the operation has a consistent and unchanging view of the source sequence for the duration of the operation. This could be satisfied by the caller holding a lock during the operation's call, by using an immutable source sequence, or by not sharing the source sequence across threads.
Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.
Unless otherwise noted, passing a null
argument to a constructor or method in this class will cause a NullPointerException
to be thrown.
As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder
. The StringBuilder
class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
Summary
Public constructors |
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
|
Constructs a string buffer with no characters in it and the specified initial capacity.
|
Constructs a string buffer that contains the same characters as the specified CharSequence .
|
Constructs a string buffer initialized to the contents of the specified string.
|
Public constructors
StringBuffer
StringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
StringBuffer
StringBuffer(capacity: Int)
Constructs a string buffer with no characters in it and the specified initial capacity.
Parameters |
capacity |
Int: the initial capacity. |
Exceptions |
java.lang.NegativeArraySizeException |
if the capacity argument is less than 0 . |
StringBuffer
StringBuffer(seq: CharSequence)
Constructs a string buffer that contains the same characters as the specified CharSequence
. The initial capacity of the string buffer is 16
plus the length of the CharSequence
argument.
If the length of the specified CharSequence
is less than or equal to zero, then an empty buffer of capacity 16
is returned.
StringBuffer
StringBuffer(str: String)
Constructs a string buffer initialized to the contents of the specified string. The initial capacity of the string buffer is 16
plus the length of the string argument.
Parameters |
str |
String: the initial contents of the buffer. |
Public methods
append
fun append(c: Char): StringBuffer
Parameters |
c |
Char: The character to append |
Exceptions |
java.io.IOException |
If an I/O error occurs |
append
fun append(s: CharSequence?): StringBuffer
Appends the specified CharSequence
to this sequence.
The characters of the CharSequence
argument are appended, in order, increasing the length of this sequence by the length of the argument.
The result of this method is exactly the same as if it were an invocation of this.append(s, 0, s.length());
This method synchronizes on this
, the destination object, but does not synchronize on the source (s
).
If s
is null
, then the four characters "null"
are appended.
Parameters |
csq |
The character sequence to append. If csq is null , then the four characters "null" are appended to this Appendable. |
s |
CharSequence?: the CharSequence to append. |
Exceptions |
java.io.IOException |
If an I/O error occurs |
append
fun append(
s: CharSequence?,
start: Int,
end: Int
): StringBuffer
Parameters |
csq |
The character sequence from which a subsequence will be appended. If csq is null , then characters will be appended as if csq contained the four characters "null" . |
start |
Int: The index of the first character in the subsequence |
end |
Int: The index of the character following the last character in the subsequence |
Exceptions |
java.lang.IndexOutOfBoundsException |
If start or end are negative, start is greater than end , or end is greater than csq.length() |
java.io.IOException |
If an I/O error occurs |
append
fun append(sb: StringBuffer?): StringBuffer
Appends the specified StringBuffer
to this sequence.
The characters of the StringBuffer
argument are appended, in order, to the contents of this StringBuffer
, increasing the length of this StringBuffer
by the length of the argument. If sb
is null
, then the four characters "null"
are appended to this StringBuffer
.
Let n be the length of the old character sequence, the one contained in the StringBuffer
just prior to execution of the append
method. Then the character at index k in the new character sequence is equal to the character at index k in the old character sequence, if k is less than n; otherwise, it is equal to the character at index k-n in the argument sb
.
This method synchronizes on this
, the destination object, but does not synchronize on the source (sb
).
capacity
fun capacity(): Int
chars
fun chars(): IntStream
Returns a stream of int
zero-extending the char
values from this sequence. Any char which maps to a surrogate code point is passed through uninterpreted.
The stream binds to this sequence when the terminal stream operation commences (specifically, for mutable sequences the spliterator for the stream is late-binding). If the sequence is modified during that operation then the result is undefined.
Return |
IntStream |
an IntStream of char values from this sequence |
codePointAt
fun codePointAt(index: Int): Int
Exceptions |
java.lang.IndexOutOfBoundsException |
|
codePointBefore
fun codePointBefore(index: Int): Int
Exceptions |
java.lang.IndexOutOfBoundsException |
|
codePointCount
fun codePointCount(
beginIndex: Int,
endIndex: Int
): Int
Exceptions |
java.lang.IndexOutOfBoundsException |
|
codePoints
fun codePoints(): IntStream
Returns a stream of code point values from this sequence. Any surrogate pairs encountered in the sequence are combined as if by Character.toCodePoint and the result is passed to the stream. Any other code units, including ordinary BMP characters, unpaired surrogates, and undefined code units, are zero-extended to int
values which are then passed to the stream.
The stream binds to this sequence when the terminal stream operation commences (specifically, for mutable sequences the spliterator for the stream is late-binding). If the sequence is modified during that operation then the result is undefined.
Return |
IntStream |
an IntStream of Unicode code points from this sequence |
compareTo
fun compareTo(other: StringBuffer): Int
Compares two StringBuffer
instances lexicographically. This method follows the same rules for lexicographical comparison as defined in the CharSequence.compare(this, another) method.
For finer-grained, locale-sensitive String comparison, refer to java.text.Collator
.
Parameters |
o |
the object to be compared. |
another |
the StringBuffer to be compared with |
Return |
Int |
the value 0 if this StringBuffer contains the same character sequence as that of the argument StringBuffer ; a negative integer if this StringBuffer is lexicographically less than the StringBuffer argument; or a positive integer if this StringBuffer is lexicographically greater than the StringBuffer argument. |
Exceptions |
java.lang.NullPointerException |
if the specified object is null |
java.lang.ClassCastException |
if the specified object's type prevents it from being compared to this object. |
delete
fun delete(
start: Int,
end: Int
): StringBuffer
Exceptions |
java.lang.StringIndexOutOfBoundsException |
|
deleteCharAt
fun deleteCharAt(index: Int): StringBuffer
Exceptions |
java.lang.StringIndexOutOfBoundsException |
|
ensureCapacity
fun ensureCapacity(minimumCapacity: Int): Unit
get
fun get(index: Int): Char
Parameters |
index |
Int: the index of the char value to be returned |
Return |
Char |
the specified char value |
Exceptions |
java.lang.IndexOutOfBoundsException |
if the index argument is negative or not less than length() |
getChars
fun getChars(
srcBegin: Int,
srcEnd: Int,
dst: CharArray!,
dstBegin: Int
): Unit
Exceptions |
java.lang.IndexOutOfBoundsException |
|
insert
fun insert(
offset: Int,
b: Boolean
): StringBuffer
Exceptions |
java.lang.StringIndexOutOfBoundsException |
|
insert
fun insert(
offset: Int,
c: Char
): StringBuffer
Exceptions |
java.lang.IndexOutOfBoundsException |
|
insert
fun insert(
offset: Int,
d: Double
): StringBuffer
Exceptions |
java.lang.StringIndexOutOfBoundsException |
|
insert
fun insert(
offset: Int,
f: Float
): StringBuffer
Exceptions |
java.lang.StringIndexOutOfBoundsException |
|
insert
fun insert(
offset: Int,
i: Int
): StringBuffer
Exceptions |
java.lang.StringIndexOutOfBoundsException |
|
insert
fun insert(
offset: Int,
obj: Any?
): StringBuffer
Exceptions |
java.lang.StringIndexOutOfBoundsException |
|
insert
fun insert(
offset: Int,
str: String?
): StringBuffer
Exceptions |
java.lang.StringIndexOutOfBoundsException |
|
insert
fun insert(
offset: Int,
l: Long
): StringBuffer
Exceptions |
java.lang.StringIndexOutOfBoundsException |
|
lastIndexOf
fun lastIndexOf(str: String): Int
lastIndexOf
fun lastIndexOf(
str: String,
fromIndex: Int
): Int
offsetByCodePoints
fun offsetByCodePoints(
index: Int,
codePointOffset: Int
): Int
Exceptions |
java.lang.IndexOutOfBoundsException |
|
replace
fun replace(
start: Int,
end: Int,
str: String
): StringBuffer
Exceptions |
java.lang.StringIndexOutOfBoundsException |
|
setCharAt
fun setCharAt(
index: Int,
ch: Char
): Unit
Exceptions |
java.lang.IndexOutOfBoundsException |
|
setLength
fun setLength(newLength: Int): Unit
Exceptions |
java.lang.IndexOutOfBoundsException |
|
subSequence
fun subSequence(
startIndex: Int,
endIndex: Int
): CharSequence
Parameters |
start |
the start index, inclusive |
end |
the end index, exclusive |
Exceptions |
java.lang.IndexOutOfBoundsException |
if start or end are negative, if end is greater than length() , or if start is greater than end |
substring
fun substring(start: Int): String
Exceptions |
java.lang.StringIndexOutOfBoundsException |
|
substring
fun substring(
start: Int,
end: Int
): String
Exceptions |
java.lang.StringIndexOutOfBoundsException |
|
toString
fun toString(): String
Return |
String |
a string consisting of exactly this sequence of characters |
trimToSize
fun trimToSize(): Unit
Properties
length
val length: Int
Return |
Int |
the number of char s in this sequence |