buildString

inline fun buildString(builderAction: StringBuilder.() -> Unit): String(source)

Builds an immutable String using an initially empty StringBuilder.

The action runs once with the builder as its receiver. Exceptions from the action propagate to the caller. As an inline action, it supports non-local returns.

val message = buildString {
append("Hello, ")
append("world!")
}

Return

an independent snapshot of the content after the action completes normally.

Parameters

builderAction

action that writes the result into the builder.


inline fun buildString(capacity: Int, builderAction: StringBuilder.() -> Unit): String(source)

Builds an immutable String with a builder sized for the expected content.

Capacity is an allocation hint, not a length limit: the builder starts empty and grows as needed. The action runs once with the builder as its receiver; its exceptions propagate to the caller. The inline action supports non-local returns.

Return

an independent snapshot of the content after the action completes normally.

Parameters

capacity

initial buffer size in UTF-16 code units.

builderAction

action that writes the result into the builder.

Throws

if capacity is negative, before the action runs.