Spec-Zone.ru › Kotlin 2

codePointAt

kotlin-stdlib/kotlin.text/codePointAt

Начиная с Kotlin: 1.0

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

Возвращает символ (кодовую точку Unicode) по указанному индексу.

Эта функция делегирует вызов java.lang.String.codePointAt; подробнее о поведении функции см. в соответствующей документации.

Параметры

index

индекс Char, значение кодовой точки которого требуется получить.

Исключения

IndexOutOfBoundsException

если индекс отрицательный или больше либо равен длине этой строки.

Примеры

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
}

© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.text/code-point-at.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API