Google is committed to advancing racial equity for Black communities. See how.
Added in API level 1

StringBuffer

class StringBuffer : Appendable, CharSequence, Serializable
kotlin.Any
   ↳ java.lang.StringBuffer

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.

<init>(capacity: Int)

Constructs a string buffer with no characters in it and the specified initial capacity.

<init>(str: String)

Constructs a string buffer initialized to the contents of the specified string.

Constructs a string buffer that contains the same characters as the specified CharSequence.

Public methods
StringBuffer
append(obj: Any?)

StringBuffer
append(str: String?)

StringBuffer

Appends the specified StringBuffer to this sequence.

StringBuffer

Appends the specified CharSequence to this sequence.

StringBuffer
append(s: CharSequence?, start: Int, end: Int)

StringBuffer

StringBuffer
append(str: CharArray!, offset: Int, len: Int)

StringBuffer

StringBuffer

StringBuffer
append(i: Int)

StringBuffer
append(lng: Long)

StringBuffer

StringBuffer

StringBuffer
appendCodePoint(codePoint: Int)

Int

Int
codePointAt(index: Int)

Int

Int
codePointCount(beginIndex: Int, endIndex: Int)

StringBuffer
delete(start: Int, end: Int)

StringBuffer
deleteCharAt(index: Int)

Unit
ensureCapacity(minimumCapacity: Int)

Char
get(index: Int)

Unit
getChars(srcBegin: Int, srcEnd: Int, dst: CharArray!, dstBegin: Int)

Int

Int
indexOf(str: String, fromIndex: Int)

StringBuffer
insert(index: Int, str: CharArray!, offset: Int, len: Int)

StringBuffer
insert(offset: Int, obj: Any?)

StringBuffer
insert(offset: Int, str: String?)

StringBuffer
insert(offset: Int, str: CharArray!)

StringBuffer
insert(dstOffset: Int, s: CharSequence?)

StringBuffer
insert(dstOffset: Int, s: CharSequence?, start: Int, end: Int)

StringBuffer
insert(offset: Int, b: Boolean)

StringBuffer
insert(offset: Int, c: Char)

StringBuffer
insert(offset: Int, i: Int)

StringBuffer
insert(offset: Int, l: Long)

StringBuffer
insert(offset: Int, f: Float)

StringBuffer
insert(offset: Int, d: Double)

Int

Int
lastIndexOf(str: String, fromIndex: Int)

Int
offsetByCodePoints(index: Int, codePointOffset: Int)

StringBuffer
replace(start: Int, end: Int, str: String)

StringBuffer

Unit
setCharAt(index: Int, ch: Char)

Unit
setLength(newLength: Int)

CharSequence
subSequence(startIndex: Int, endIndex: Int)

String
substring(start: Int)

String
substring(start: Int, end: Int)

String

Unit

Properties
Int

Public constructors

<init>

Added in API level 1
StringBuffer()

Constructs a string buffer with no characters in it and an initial capacity of 16 characters.

<init>

Added in API level 1
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.

<init>

Added in API level 1
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.

<init>

Added in API level 1
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.

Parameters
seq CharSequence: the sequence to copy.

Public methods

append

Added in API level 1
fun append(obj: Any?): StringBuffer

append

Added in API level 1
fun append(str: String?): StringBuffer

append

Added in API level 1
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).

Parameters
sb StringBuffer?: the StringBuffer to append.
Return
StringBuffer a reference to this object.

append

Added in API level 1
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.
Return
StringBuffer a reference to this object.
Exceptions
java.io.IOException If an I/O error occurs

append

Added in API level 1
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
Return
StringBuffer A reference to this Appendable
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

Added in API level 1
fun append(str: CharArray!): StringBuffer

append

Added in API level 1
fun append(
    str: CharArray!,
    offset: Int,
    len: Int
): StringBuffer
Exceptions
java.lang.IndexOutOfBoundsException

append

Added in API level 1
fun append(b: Boolean): StringBuffer

append

Added in API level 1
fun append(c: Char): StringBuffer
Parameters
c Char: The character to append
Return
StringBuffer A reference to this Appendable
Exceptions
java.io.IOException If an I/O error occurs

append

Added in API level 1
fun append(i: Int): StringBuffer

append

Added in API level 1
fun append(lng: Long): StringBuffer

append

Added in API level 1
fun append(f: Float): StringBuffer

append

Added in API level 1
fun append(d: Double): StringBuffer

appendCodePoint

Added in API level 1
fun appendCodePoint(codePoint: Int): StringBuffer

capacity

fun capacity(): Int

codePointAt

Added in API level 1.5
fun codePointAt(index: Int): Int

codePointBefore

Added in API level 1.5
fun codePointBefore(index: Int): Int

codePointCount

Added in API level 1.5
fun codePointCount(
    beginIndex: Int,
    endIndex: Int
): Int

delete

Added in API level 1
fun delete(
    start: Int,
    end: Int
): StringBuffer
Exceptions
java.lang.StringIndexOutOfBoundsException

deleteCharAt

Added in API level 1
fun deleteCharAt(index: Int): StringBuffer
Exceptions
java.lang.StringIndexOutOfBoundsException

ensureCapacity

fun ensureCapacity(minimumCapacity: Int): Unit

get

Added in API level 1
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()

See Also

getChars

fun getChars(
    srcBegin: Int,
    srcEnd: Int,
    dst: CharArray!,
    dstBegin: Int
): Unit
Exceptions
java.lang.IndexOutOfBoundsException

indexOf

Added in API level 1.4
fun indexOf(str: String): Int

indexOf

Added in API level 1.4
fun indexOf(
    str: String,
    fromIndex: Int
): Int

insert

Added in API level 1
fun insert(
    index: Int,
    str: CharArray!,
    offset: Int,
    len: Int
): StringBuffer
Exceptions
java.lang.StringIndexOutOfBoundsException

insert

Added in API level 1
fun insert(
    offset: Int,
    obj: Any?
): StringBuffer
Exceptions
java.lang.StringIndexOutOfBoundsException

insert

Added in API level 1
fun insert(
    offset: Int,
    str: String?
): StringBuffer
Exceptions
java.lang.StringIndexOutOfBoundsException

insert

Added in API level 1
fun insert(
    offset: Int,
    str: CharArray!
): StringBuffer
Exceptions
java.lang.StringIndexOutOfBoundsException

insert

Added in API level 1
fun insert(
    dstOffset: Int,
    s: CharSequence?
): StringBuffer
Exceptions
java.lang.IndexOutOfBoundsException

insert

Added in API level 1
fun insert(
    dstOffset: Int,
    s: CharSequence?,
    start: Int,
    end: Int
): StringBuffer
Exceptions
java.lang.IndexOutOfBoundsException

insert

Added in API level 1
fun insert(
    offset: Int,
    b: Boolean
): StringBuffer
Exceptions
java.lang.StringIndexOutOfBoundsException

insert

Added in API level 1
fun insert(
    offset: Int,
    c: Char
): StringBuffer
Exceptions
java.lang.IndexOutOfBoundsException

insert

Added in API level 1
fun insert(
    offset: Int,
    i: Int
): StringBuffer
Exceptions
java.lang.StringIndexOutOfBoundsException

insert

Added in API level 1
fun insert(
    offset: Int,
    l: Long
): StringBuffer
Exceptions
java.lang.StringIndexOutOfBoundsException

insert

Added in API level 1
fun insert(
    offset: Int,
    f: Float
): StringBuffer
Exceptions
java.lang.StringIndexOutOfBoundsException

insert

Added in API level 1
fun insert(
    offset: Int,
    d: Double
): StringBuffer
Exceptions
java.lang.StringIndexOutOfBoundsException

lastIndexOf

Added in API level 1.4
fun lastIndexOf(str: String): Int

lastIndexOf

Added in API level 1.4
fun lastIndexOf(
    str: String,
    fromIndex: Int
): Int

offsetByCodePoints

Added in API level 1.5
fun offsetByCodePoints(
    index: Int,
    codePointOffset: Int
): Int

replace

Added in API level 1
fun replace(
    start: Int,
    end: Int,
    str: String
): StringBuffer
Exceptions
java.lang.StringIndexOutOfBoundsException

reverse

Added in API level 1
fun reverse(): StringBuffer

setCharAt

fun setCharAt(
    index: Int,
    ch: Char
): Unit
Exceptions
java.lang.IndexOutOfBoundsException

See Also

setLength

fun setLength(newLength: Int): Unit
Exceptions
java.lang.IndexOutOfBoundsException

See Also

subSequence

Added in API level 1
fun subSequence(
    startIndex: Int,
    endIndex: Int
): CharSequence
Parameters
start the start index, inclusive
end the end index, exclusive
Return
CharSequence the specified subsequence
Exceptions
java.lang.IndexOutOfBoundsException if start or end are negative, if end is greater than length(), or if start is greater than end

substring

Added in API level 1.2
fun substring(start: Int): String
Exceptions
java.lang.StringIndexOutOfBoundsException

substring

Added in API level 1.2
fun substring(
    start: Int,
    end: Int
): String
Exceptions
java.lang.StringIndexOutOfBoundsException

toString

Added in API level 1
fun toString(): String
Return
String a string consisting of exactly this sequence of characters

trimToSize

Added in API level 1.5
fun trimToSize(): Unit

Properties

length

Added in API level 1
val length: Int
Return
Int the number of chars in this sequence