codePointBefore

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

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

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

Since Kotlin

1.0

Parameters

index

the index of a Char which follows a codepoint value that will be returned.

Throws

if the index is less than 1, or exceeds the 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.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
}