takeWhile

inline fun CharSequence.takeWhile(predicate: (Char) -> Boolean): CharSequence(source)

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

Since Kotlin

1.0

Samples

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

fun main() { 
   //sampleStart 
   val string = "<<>>"
println(string.take(8)) // <<>>
println(string.takeWhile { !it.isLetter() }) // <<<
println(string.takeLastWhile { !it.isLetter() }) // >>> 
   //sampleEnd
}

inline fun String.takeWhile(predicate: (Char) -> Boolean): String(source)

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

Since Kotlin

1.0

Samples

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

fun main() { 
   //sampleStart 
   val string = "<<>>"
println(string.take(8)) // <<>>
println(string.takeWhile { !it.isLetter() }) // <<<
println(string.takeLastWhile { !it.isLetter() }) // >>> 
   //sampleEnd
}