setCodePointAt

Replaces the code point at the specified index in this StringBuilder with the specified value and returns this instance.

Note that the length of the StringBuilder can change in the result of this operation, for example, when a code point that is represented by a single char is replaced with a code point represented by a surrogate pair of chars and vice versa.

Since Kotlin

2.5

Parameters

index

The position of the code point to replace.

value

The new code point to set at the specified position.

See also

Throws

if index is less than zero or greater than or equal to the length of this string builder.

Samples

import kotlin.text.unicode.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val builder = StringBuilder("๐Ÿ˜๐Ÿ˜ Kotlin")
println(builder.length) // 11 // 2 supplementary code points and 7 basic ones
val position = builder.offsetByCodePoints(0, codePointOffset = 1)
builder.setCodePointAt(position, '+'.toCodePoint())
println(builder.toString()) // ๐Ÿ˜+ Kotlin
println(builder.length) // 10 // 1 supplementary code points and 8 basic ones

val position2 = builder.offsetByCodePoints(position, codePointOffset = 1)
builder.setCodePointAt(position2, CodePoint(0x1F60B))
println(builder.toString()) // ๐Ÿ˜+๐Ÿ˜‹Kotlin
println(builder.length) // 11 // again 2 supplementary code points and 7 basic ones 
   //sampleEnd
}