last
Returns the last character.
Since Kotlin
1.0Throws
if the char sequence is empty.
Samples
import java.util.Locale
import kotlin.test.*
fun main() {
//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
}
Returns the last character matching the given predicate.
Since Kotlin
1.0Throws
if no such character is found.
Samples
import java.util.Locale
import kotlin.test.*
fun main() {
//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
}