takeLastWhile

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

Returns a subsequence of this char sequence containing last characters that satisfy the given predicate.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val chars = ('a'..'z').toList()
println(chars.take(3)) // [a, b, c]
println(chars.takeWhile { it < 'f' }) // [a, b, c, d, e]
println(chars.takeLast(2)) // [y, z]
println(chars.takeLastWhile { it > 'w' }) // [x, y, z]
//sampleEnd
}
Common
JVM
JS
Native
1.0
inline fun String.takeLastWhile(
    predicate: (Char) -> Boolean
): String

Returns a string containing last characters that satisfy the given predicate.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val chars = ('a'..'z').toList()
println(chars.take(3)) // [a, b, c]
println(chars.takeWhile { it < 'f' }) // [a, b, c, d, e]
println(chars.takeLast(2)) // [y, z]
println(chars.takeLastWhile { it > 'w' }) // [x, y, z]
//sampleEnd
}