codePointAt

inline fun String.codePointAt(index: Int): Int(source)

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

This function delegates to java.lang.String.codePointAt, refer to the corresponding documentation for more details on function's behavior.

Since Kotlin

1.0

Parameters

index

the index of Char whose codepoint value is needed.

Throws

if the index is negative or greater or equal to length of this string.

Samples

import kotlin.test.*
import java.util.*
import java.util.regex.*

fun main() { 
   //sampleStart 
   val str = "abc"
// 'a'.code == 97
println(str.codePointAt(0).toString()) // 97
// 'b'.code == 98
println(str.codePointAt(1).toString()) // 98
// 'c'.code == 99
println(str.codePointAt(2).toString()) // 99
// 3 is out of the str bounds
// str.codePointAt(3) // will fail with IndexOutOfBoundsException
// index is negative
// str.codePointAt(-1) // will fail with IndexOutOfBoundsException

val broccoli = "πŸ₯¦"
// πŸ₯¦ has a code point value 0x1F966 (129382 in decimal), and it is represented as a UTF-16 surrogate pair 0xD83E, 0xDD66 (or 55358, 56678 in decimal)
// Returns a code point value corresponding to the surrogate pair with a high surrogate at index 0
println(broccoli.codePointAt(0)) // 129382
// Returns a code point value corresponding to the low surrogate
println(broccoli.codePointAt(1)) // 56678 
   //sampleEnd
}