splitToSequence

fun CharSequence.splitToSequence(vararg delimiters: String, ignoreCase: Boolean = false, limit: Int = 0): Sequence<String>(source)

Splits this char sequence to a sequence of strings around occurrences of the specified delimiters.

Since Kotlin

1.0

Parameters

delimiters

One or more strings to be used as delimiters.

ignoreCase

true to ignore character case when matching a delimiter. By default false.

limit

The maximum number of substrings to return. Zero by default means no limit is set.

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.


fun CharSequence.splitToSequence(vararg delimiters: Char, ignoreCase: Boolean = false, limit: Int = 0): Sequence<String>(source)

Splits this char sequence to a sequence of strings around occurrences of the specified delimiters.

Since Kotlin

1.0

Parameters

delimiters

One or more characters to be used as delimiters.

ignoreCase

true to ignore character case when matching a delimiter. By default false.

limit

The maximum number of substrings to return.


inline fun CharSequence.splitToSequence(regex: Regex, limit: Int = 0): Sequence<String>(source)

Splits this char sequence to a sequence of strings around matches of the given regular expression.

Since Kotlin

1.6

Parameters

limit

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

Samples

import java.util.Locale
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
}