splitToSequence
Splits this char sequence to a sequence of strings around occurrences of the specified delimiters.
The last element of the resulting sequence corresponds to a subsequence starting right after the last delimiter occurrence (or at the beginning of this char sequence if there were no such occurrences) and ending at the end of this char sequence. That implies that if this char sequence does not contain delimiters, the resulting sequence will contain a single element corresponding to the whole char sequence. It also implies that for char sequences ending with one of delimiters, the resulting sequence will end with an empty string.
To avoid ambiguous results when strings in delimiters have characters in common, this method proceeds from the beginning to the end of this string, and finds at each position the first element in delimiters that matches this string at that position.
Since Kotlin
1.0Parameters
One or more strings to be used as delimiters.
true to ignore character case when matching a delimiter. By default false.
The maximum number of substrings to return. Zero by default means no limit is set.
Samples
import kotlin.test.*
fun main() {
//sampleStart
fun Sequence<String>.toPrettyString() = joinToString(", ", "[", "]")
val multiCharDelimiter = "apple--banana--cherry".splitToSequence("--").toPrettyString()
println(multiCharDelimiter) // [apple, banana, cherry]
val multipleSplit = "apple->banana;;cherry:::orange".splitToSequence("->", ";;", ":::").toPrettyString()
println(multipleSplit) // [apple, banana, cherry, orange]
val longerDelimiterFirst = "apple<-banana<--cherry".splitToSequence("<--", "<-").toPrettyString()
println(longerDelimiterFirst) // [apple, banana, cherry]
val shorterDelimiterFirst = "apple<-banana<--cherry".splitToSequence("<-", "<--").toPrettyString()
println(shorterDelimiterFirst) // [apple, banana, -cherry]
val limitSplit = "a->b->c->d->e".splitToSequence("->", limit = 3).toPrettyString()
println(limitSplit) // [a, b, c->d->e]
val emptyInputResult = "".splitToSequence("sep").toList()
println("emptyInputResult == listOf(\"\") is ${emptyInputResult == listOf("")}") // true
val emptyDelimiterSplit = "abc".splitToSequence("").toPrettyString()
println(emptyDelimiterSplit) // [, a, b, c, ]
val mixedCase = "abcXYZdef".splitToSequence("xyz").toPrettyString()
println(mixedCase) // [abcXYZdef] // No match with case sensitivity
val mixedCaseIgnored = "abcXYZdef".splitToSequence("xyz", ignoreCase = true).toPrettyString()
println(mixedCaseIgnored) // [abc, def] // Matches with case insensitivity
val emptyResults = "##a##b##c##".splitToSequence("##").toPrettyString()
println(emptyResults) // [, a, b, c, ]
val consecutiveSeparators = "a--b------c".splitToSequence("--").toPrettyString()
println(consecutiveSeparators) // [a, b, , , c]
//sampleEnd
}Splits this char sequence to a sequence of strings around occurrences of the specified delimiters.
The last element of the resulting sequence corresponds to a subsequence starting right after the last delimiter occurrence (or at the beginning of this char sequence if there were no such occurrences) and ending at the end of this char sequence. That implies that if this char sequence does not contain delimiters, the resulting sequence will contain a single element corresponding to the whole char sequence. It also implies that for char sequences ending with one of delimiters, the resulting sequence will end with an empty string.
Since Kotlin
1.0Parameters
One or more characters to be used as delimiters.
true to ignore character case when matching a delimiter. By default false.
The maximum number of substrings to return.
Samples
import kotlin.test.*
fun main() {
//sampleStart
fun Sequence<String>.toPrettyString() = joinToString(", ", "[", "]")
val commaSplit = "apple,banana,cherry".splitToSequence(',').toPrettyString()
println(commaSplit) // [apple, banana, cherry]
val charSplit = "apple,banana;cherry".splitToSequence(',', ';').toPrettyString()
println(charSplit) // [apple, banana, cherry]
val limitSplit = "a,b,c,d,e".splitToSequence(',', limit = 3).toPrettyString()
println(limitSplit) // [a, b, c,d,e]
val emptyInputResult = "".splitToSequence('|').toList()
println("emptyInputResult == listOf(\"\") is ${emptyInputResult == listOf("")}") // true
val mixedCase = "abcXdef".splitToSequence('x').toPrettyString()
println(mixedCase) // [abcXdef] // No match with case sensitivity
val mixedCaseIgnored = "abcXdef".splitToSequence('x', ignoreCase = true).toPrettyString()
println(mixedCaseIgnored) // [abc, def] // Matches with case insensitivity
val emptyResults = ",a,b,c,".splitToSequence(',').toPrettyString()
println(emptyResults) // [, a, b, c, ]
val consecutiveSeparators = "a,,b,,,c".splitToSequence(',').toPrettyString()
println(consecutiveSeparators) // [a, , b, , , c]
//sampleEnd
}Splits this char sequence to a sequence of strings around matches of the given regular expression.
The last element of the resulting sequence corresponds to a subsequence starting right after the last regex match (or at the beginning of this char sequence if there were no matches) and ending at the end of this char sequence. That implies that if this char sequences does not contain subsequences matching regex, the resulting sequence will contain a single element corresponding to the whole char sequence. It also implies that for char sequences ending with a regex match, the resulting sequence will end with an empty string.
Since Kotlin
1.6Parameters
Non-negative value specifying the maximum number of substrings to return. Zero by default means no limit is set.
Samples
import kotlin.test.*
fun main() {
//sampleStart
val colors = "green, red , brown&blue, orange, pink&green"
val regex = "[,\\s]+".toRegex()
val mixedColor = colors.splitToSequence(regex)
.onEach { println(it) }
.firstOrNull { it.contains('&') }
println(mixedColor) // brown&blue
//sampleEnd
}