toSurrogatePair

Converts this supplementary code point to a surrogate pair.

This method is only applicable to supplementary code points in the range 0x10000 to 0x10FFFF. The returned pair contains the high surrogate as the first component and the low surrogate as the second component.

Since Kotlin

2.5

Return

A Pair of Char values representing the high and low surrogates.

See also

Throws

if this is a basic (BMP) code point.

Samples

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

fun main() { 
   //sampleStart 
   val supplementaryCodePoint = CodePoint(0x1F60B) // πŸ˜‹
val [high, low] = supplementaryCodePoint.toSurrogatePair()

// this code point is represented by surrogate pair \uD83D \uDE0B
println(high.code.toHexString()) // 0000d83d
println(low.code.toHexString()) // 0000de0b
// or
val string = supplementaryCodePoint.toSurrogatePair { h, l -> "$h$l" }
println(string) // πŸ˜‹

val basicCodePoint = 'Ü'.toCodePoint()
// basicCodePoint.toSurrogatePair() // will fail with IllegalArgumentException 
   //sampleEnd
}

inline fun <T> toSurrogatePair(action: (high: Char, low: Char) -> T): T(source)

Converts this supplementary code point to a surrogate pair and applies the given action to its components.

This method is only applicable to supplementary code points in the range 0x10000 to 0x10FFFF. The action function receives the high surrogate as the first parameter and the low surrogate as the second parameter.

Since Kotlin

2.5

Return

The result of applying action to the high and low surrogates.

Parameters

action

A function that takes the high and low surrogate Char values and returns a result of type T.

See also

Throws

if this is a basic (BMP) code point.

Samples

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

fun main() { 
   //sampleStart 
   val supplementaryCodePoint = CodePoint(0x1F60B) // πŸ˜‹
val [high, low] = supplementaryCodePoint.toSurrogatePair()

// this code point is represented by surrogate pair \uD83D \uDE0B
println(high.code.toHexString()) // 0000d83d
println(low.code.toHexString()) // 0000de0b
// or
val string = supplementaryCodePoint.toSurrogatePair { h, l -> "$h$l" }
println(string) // πŸ˜‹

val basicCodePoint = 'Ü'.toCodePoint()
// basicCodePoint.toSurrogatePair() // will fail with IllegalArgumentException 
   //sampleEnd
}