StringBuilder

class StringBuilder(capacity: Int = 16) : CharSequence(source)

Builds mutable text by appending UTF-16 code units.

length counts code units, so a supplementary Unicode character occupies two indexes. Capacity is the allocated buffer size, not the content length. clear retains that buffer; toString and subSequence return independent immutable snapshots. Buffer growth and snapshots are subject to the Guest heap quota.

Parameters

capacity

initial buffer size in UTF-16 code units; defaults to 16.

See also

Throws

Constructors

Link copied to clipboard
constructor(capacity: Int = 16)

Creates an empty builder with the requested initial capacity.

Properties

Link copied to clipboard
open override val length: Int

Number of UTF-16 code units currently stored, excluding unused capacity.

Functions

Link copied to clipboard
fun append(value: Any?): StringBuilder

Converts value to text and appends that text.

Appends one UTF-16 code unit, growing the buffer if needed.

Copies all code units from value to the end of this builder.

fun append(value: CharSequence?, startIndex: Int, endIndex: Int): StringBuilder

Copies a range of value to the end of this builder.

Link copied to clipboard

Appends a line feed (\n).

Converts value to text, appends it, then appends a line feed (\n).

Appends value followed by a line feed (\n).

Copies value followed by a line feed (\n). Null appends null before the line feed.

Link copied to clipboard
fun appendRange(value: CharArray, startIndex: Int, endIndex: Int): StringBuilder

Copies a range of value to the end of this builder.

Link copied to clipboard
fun capacity(): Int

Returns the number of UTF-16 code units the buffer can hold without growing.

Link copied to clipboard

Removes all content while retaining the allocated capacity for reuse.

Link copied to clipboard

Returns whether this string contains char.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun ensureCapacity(minimumCapacity: Int)

Grows the buffer when minimumCapacity exceeds the current capacity.

Link copied to clipboard
open operator override fun get(index: Int): Char

Reads a UTF-16 code unit at index.

Link copied to clipboard
fun CharSequence.indexOf(char: Char, startIndex: Int = 0): Int

Finds the first matching character at or after startIndex, clamped to zero.

fun CharSequence.indexOf(other: String, startIndex: Int = 0): Int
Link copied to clipboard

Returns whether this character sequence is empty or contains only whitespace.

Link copied to clipboard

Returns whether this character sequence contains no characters.

Link copied to clipboard

Returns whether this character sequence contains a non-whitespace character.

Link copied to clipboard

Returns whether this character sequence contains at least one character.

Link copied to clipboard

Visits UTF-16 code units in order. Stored iterators are supported and own their position.

Link copied to clipboard

Finds the last matching character.

Finds the last matching substring, starting at the last character.

fun CharSequence.lastIndexOf(char: Char, startIndex: Int): Int

Searches backwards from startIndex for char.

fun CharSequence.lastIndexOf(string: String, startIndex: Int): Int

Searches backwards from startIndex for string, comparing UTF-16 code units.

Link copied to clipboard

Splits on CRLF, LF or CR, retaining empty lines including the final line.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Removes suffix when present, otherwise returns this string.

Link copied to clipboard
Link copied to clipboard
operator fun set(index: Int, value: Char)

Replaces one UTF-16 code unit without changing the content length.

Link copied to clipboard
fun setLength(newLength: Int)

Changes the content length, growing the buffer if needed.

Link copied to clipboard
fun CharSequence.split(delimiter: Char, limit: Int = 0): List<String>

Splits on delimiter, retaining empty parts. Zero limit means no limit; positive limit keeps the remaining suffix.

fun CharSequence.split(delimiter: String, limit: Int = 0): List<String>

Splits on delimiter, retaining empty parts. An empty delimiter separates UTF-16 code units, including empty ends.

Link copied to clipboard
Link copied to clipboard
open override fun subSequence(startIndex: Int, endIndex: Int): String

Copies the selected range into an immutable String.

Link copied to clipboard
fun CharSequence.substring(startIndex: Int): String

fun CharSequence.substring(startIndex: Int, endIndex: Int): String

Materializes the selected UTF-16 range as an immutable String.

Link copied to clipboard

Counts and indexes refer to UTF-16 code units, including individual surrogate halves.

Link copied to clipboard
Link copied to clipboard
open override fun toString(): String

Copies the current content into an immutable String.

Link copied to clipboard

Removes leading and trailing whitespace.

Link copied to clipboard

Removes trailing whitespace.

Link copied to clipboard

Removes leading whitespace.