last

Common
JVM
JS
Native
1.0
fun CharSequence.last(): Char
(source)

Returns the last character.

import java.util.Locale
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val string = "Kotlin 1.4.0"
println(string.last()) // 0
println(string.last { it.isLetter() }) // n
println(string.lastOrNull { it > 'z' }) // null
// string.last { it > 'z' } //  will fail

val emptyString = ""
println(emptyString.lastOrNull()) // null
// emptyString.last() //  will fail
//sampleEnd
}

Exceptions

NoSuchElementException - if the char sequence is empty.

Common
JVM
JS
Native
1.0
inline fun CharSequence.last(
    predicate: (Char) -> Boolean
): Char

(source)

Returns the last character matching the given predicate.

import java.util.Locale
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val string = "Kotlin 1.4.0"
println(string.last()) // 0
println(string.last { it.isLetter() }) // n
println(string.lastOrNull { it > 'z' }) // null
// string.last { it > 'z' } //  will fail

val emptyString = ""
println(emptyString.lastOrNull()) // null
// emptyString.last() //  will fail
//sampleEnd
}

Exceptions

NoSuchElementException - if no such character is found.