codePointCount
Returns the number of Unicode code points in the specified text range of this String.
This function delegates to java.lang.String.codePointCount
, refer to the corresponding documentation for more details on function's behavior.
Since Kotlin
1.0Parameters
beginIndex
the index of a Char corresponding to a beginning of the text range (inclusive).
endIndex
the index of a Char corresponding to an end of the text range (exclusive).
Throws
when either of the indices is negative, exceeds length of this string, or when beginIndex is greater than endIndex.
Samples
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
}