Код: Выделить всё
val foo = "111222111"
val prefix = foo.takeWhile { it == '1' } // "111"
val suffix = foo.dropWhile { it == '1' } // "222111"
// Is there a `span` method that returns both
// the matching prefix and the non-matching suffix
// with one call?
val (p, s) = foo.span { it == '1' }
< /code>
Редактировать:
Самая простая реализация, которую я придумал: < /p>
fun String.span(predicate: (Char) -> Boolean) =
this.takeWhile(predicate).let { prefix ->
Pair(prefix, this.drop(prefix.length)) }
Подробнее здесь: https://stackoverflow.com/questions/795 ... d-dropwhil
Мобильная версия