Spec-Zone.ru › Kotlin 2

codePointBefore

kotlin-stdlib/kotlin.text/codePointBefore

Начиная с Kotlin: 1.0

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

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

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

Параметры

index

индекс Char, следующего за возвращаемым значением кодовой точки.

Исключения

IndexOutOfBoundsException

если index меньше 1 или превышает длину этой строки.

Примеры

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

fun main() { 
   //sampleStart 
   val str = "abc"
// 'a'.code == 97
println(str.codePointBefore(1).toString()) // 97
// 'b'.code == 98
println(str.codePointBefore(2).toString()) // 98
// 'c'.code == 99
println(str.codePointBefore(3).toString()) // 99
// There are no code points prior to index 0
// str.codePointBefore(0) // will fail with IndexOutOfBoundsException
// The index is negative
// str.codePointBefore(-1) // will fail with IndexOutOfBoundsException
// The index exceeds the length of the string
// str.codePointBefore(str.length + 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 high surrogate
println(broccoli.codePointBefore(1)) // 55358
// Returns a code point value corresponding to the whole surrogate pair
println(broccoli.codePointBefore(2)) // 129382 
   //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-before.html

Spec-Zone.ru

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