code Point Count
Версия Kotlin: 1.0
inline fun String.codePointCount(beginIndex: Int, endIndex: Int): Int
Возвращает количество кодовых точек Unicode в указанном диапазоне текста этой строки.
Эта функция делегирует вызов java.lang.String.codePointCount. Подробнее о поведении функции см. в соответствующей документации.
Параметры
begin Index
индекс Char, соответствующего началу диапазона текста (включительно).
end Index
индекс Char, соответствующего концу диапазона текста (не включительно).
Исключения
если один из индексов отрицательный, превышает длину этой строки или если beginIndex больше endIndex.
Примеры
import kotlin.test.*
import java.util.*
import java.util.regex.*
fun main() {
//sampleStart
val str = "abc"
// The string contains three code points: 97, 98 and 99
println(str.codePointCount(0, 3).toString()) // 3
// There are two code points in between characters with code points 1 (inclusive) and 3 (exclusive)
println(str.codePointCount(1, 3).toString()) // 2
// There are no code points for an empty range
println(str.codePointCount(2, 2).toString()) // 0
// The begin index cannot exceed the end index
// str.codePointCount(3, 2) // will fail with IndexOutOfBoundsException
// Indices cannot be negative
// str.codePointCount(-1, 2) // will fail with IndexOutOfBoundsException
// The end index cannot exceed the length of the string
// str.codePointCount(0, str.length + 1) // will fail with IndexOutOfBoundsException
val broccoli = "🥦"
// 🥦 has a code point value 0x1F966, and it is represented as a UTF-16 surrogate pair 0xD83E, 0xDD66
// The surrogate pair is counted as a single code point
println(broccoli.codePointCount(0, broccoli.length /* = 2 */)) // 1
// The high-surrogate char is counted as a single code point as well
println(broccoli.codePointCount(0, broccoli.length - 1 /* = 1 */)) // 1
//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-count.html