Added in API level 1

StringBuilder

class StringBuilder : Appendable, CharSequence, Comparable<StringBuilder!>, Serializable
kotlin.Any
   ↳ java.lang.StringBuilder

A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

The principal operations on a StringBuilder 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 builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.

For example, if z refers to a string builder object whose current contents are "start", then the method call z.append("le") would cause the string builder to contain "startle", whereas z.insert(4, "le") would alter the string builder to contain "starlet".

In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that java.lang.StringBuffer be used.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

Summary

Public constructors

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

StringBuilder(capacity: Int)

Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.

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

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

Public methods
StringBuilder

StringBuilder

Appends the specified character to this Appendable.

StringBuilder

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

StringBuilder

StringBuilder

StringBuilder
append(i: Int)

StringBuilder

Appends the specified character sequence to this Appendable.

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

StringBuilder
append(obj: Any?)

StringBuilder
append(str: String?)

StringBuilder

Appends the specified StringBuffer to this sequence.

StringBuilder
append(lng: Long)

StringBuilder
appendCodePoint(codePoint: Int)

Int

Returns the current capacity.

IntStream

Returns a stream of int zero-extending the char values from this sequence.

Int
codePointAt(index: Int)

Returns the character (Unicode code point) at the specified index.

Int

Returns the character (Unicode code point) before the specified index.

Int
codePointCount(beginIndex: Int, endIndex: Int)

Returns the number of Unicode code points in the specified text range of this sequence.

IntStream

Returns a stream of code point values from this sequence.

Int

Compares two StringBuilder instances lexicographically.

StringBuilder
delete(start: Int, end: Int)

StringBuilder
deleteCharAt(index: Int)

Unit
ensureCapacity(minimumCapacity: Int)

Ensures that the capacity is at least equal to the specified minimum.

Char
get(index: Int)

Returns the char value in this sequence at the specified index.

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

Copies characters from this sequence into the given destination array.

Int

Int
indexOf(str: String, fromIndex: Int)

StringBuilder
insert(offset: Int, b: Boolean)

StringBuilder
insert(offset: Int, c: Char)

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

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

StringBuilder
insert(offset: Int, d: Double)

StringBuilder
insert(offset: Int, f: Float)

StringBuilder
insert(offset: Int, i: Int)

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

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

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

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

StringBuilder
insert(offset: Int, l: Long)

Int

Int
lastIndexOf(str: String, fromIndex: Int)

Int
offsetByCodePoints(index: Int, codePointOffset: Int)

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

StringBuilder
repeat(codePoint: Int, count: Int)

StringBuilder
repeat(cs: CharSequence, count: Int)

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

StringBuilder

Unit
setCharAt(index: Int, ch: Char)

The character at the specified index is set to ch.

Unit
setLength(newLength: Int)

Sets the length of the character sequence.

CharSequence
subSequence(startIndex: Int, endIndex: Int)

Returns a new character sequence that is a subsequence of this sequence.

String
substring(start: Int)

Returns a new String that contains a subsequence of characters currently contained in this character sequence.

String
substring(start: Int, end: Int)

Returns a new String that contains a subsequence of characters currently contained in this sequence.

String

Returns a string representation of the object.

Unit

Attempts to reduce storage used for the character sequence.

Properties
Int

Returns the length (character count).

Public constructors

StringBuilder

Added in API level 1
StringBuilder()

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

StringBuilder

Added in API level 1
StringBuilder(capacity: Int)

Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.

Parameters
capacity Int: the initial capacity.
Exceptions
java.lang.NegativeArraySizeException if the capacity argument is less than 0.

StringBuilder

Added in API level 1
StringBuilder(seq: CharSequence)

Constructs a string builder that contains the same characters as the specified CharSequence. The initial capacity of the string builder is 16 plus the length of the CharSequence argument.

Parameters
seq CharSequence: the sequence to copy.

StringBuilder

Added in API level 1
StringBuilder(str: String)

Constructs a string builder initialized to the contents of the specified string. The initial capacity of the string builder is 16 plus the length of the string argument.

Parameters
str String: the initial contents of the buffer.

Public methods

append

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

append

Added in API level 1
fun append(c: Char): StringBuilder

Appends the specified character to this Appendable.

Parameters
c Char: The character to append
Return
StringBuilder A reference to this Appendable
Exceptions
java.io.IOException If an I/O error occurs

append

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

append

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

append

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

append

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

append

Added in API level 1