splitToSequence

Platform and version requirements: JVM (1.6), JS (1.6), Native (1.6)
fun splitToSequence(
    input: CharSequence, 
    limit: Int = 0
): Sequence<String>

Splits the input CharSequence to a sequence of strings around matches of this regular expression.



fun main(args: Array<String>) {
//sampleStart
val colors = "green, red , brown&blue, orange, pink&green"
val regex = "[,\\s]+".toRegex()

val mixedColor = regex.splitToSequence(colors)
    .onEach { println(it) }
    .firstOrNull { it.contains('&') }

println(mixedColor) // brown&blue
//sampleEnd
}

Parameters

limit - Non-negative value specifying the maximum number of substrings the string can be split to. Zero by default means no limit is set.

© 2010–2021 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/split-to-sequence.html