splitToSequence

expect fun splitToSequence(input: CharSequence, limit: Int = 0): Sequence<String>(source)

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

The last element of the resulting sequence corresponds to an input subsequence starting right after the last match (or at the beginning of input char sequence if there were no matches) and ending at the end of input. That implies that if input does not contain subsequences matching this regular expression, the resulting sequence will contain a single element corresponding to the whole input sequence. It also implies that for char sequences ending with a this regular expression match, the resulting sequence will end with an empty string.

Since Kotlin

1.6

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.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   fun Sequence<String>.toPrettyString() = joinToString(", ", "[", "]")

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

val splitNonMatchingInput = regex.splitToSequence("Split_me_if_you_can").toPrettyString()
println(splitNonMatchingInput) // [Split_me_if_you_can]

val splitSurroundedByMatches = regex.splitToSequence(",(-.-),").toPrettyString()
println(splitSurroundedByMatches) // [, (-.-), ]

println("regex.splitToSequence(\"\").toList() == listOf(\"\") is ${regex.splitToSequence("").toList() == listOf("")}") // true 
   //sampleEnd
}
actual fun splitToSequence(input: CharSequence, limit: Int = 0): Sequence<String>(source)

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

The last element of the resulting sequence corresponds to an input subsequence starting right after the last match (or at the beginning of input char sequence if there were no matches) and ending at the end of input. That implies that if input does not contain subsequences matching this regular expression, the resulting sequence will contain a single element corresponding to the whole input sequence. It also implies that for char sequences ending with a this regular expression match, the resulting sequence will end with an empty string.

Since Kotlin

1.6

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.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   fun Sequence<String>.toPrettyString() = joinToString(", ", "[", "]")

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

val splitNonMatchingInput = regex.splitToSequence("Split_me_if_you_can").toPrettyString()
println(splitNonMatchingInput) // [Split_me_if_you_can]

val splitSurroundedByMatches = regex.splitToSequence(",(-.-),").toPrettyString()
println(splitSurroundedByMatches) // [, (-.-), ]

println("regex.splitToSequence(\"\").toList() == listOf(\"\") is ${regex.splitToSequence("").toList() == listOf("")}") // true 
   //sampleEnd
}
actual fun splitToSequence(input: CharSequence, limit: Int = 0): Sequence<String>(source)

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

The last element of the resulting sequence corresponds to an input subsequence starting right after the last match (or at the beginning of input char sequence if there were no matches) and ending at the end of input. That implies that if input does not contain subsequences matching this regular expression, the resulting sequence will contain a single element corresponding to the whole input sequence. It also implies that for char sequences ending with a this regular expression match, the resulting sequence will end with an empty string.

Since Kotlin

1.6

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.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   fun Sequence<String>.toPrettyString() = joinToString(", ", "[", "]")

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

val splitNonMatchingInput = regex.splitToSequence("Split_me_if_you_can").toPrettyString()
println(splitNonMatchingInput) // [Split_me_if_you_can]

val splitSurroundedByMatches = regex.splitToSequence(",(-.-),").toPrettyString()
println(splitSurroundedByMatches) // [, (-.-), ]

println("regex.splitToSequence(\"\").toList() == listOf(\"\") is ${regex.splitToSequence("").toList() == listOf("")}") // true 
   //sampleEnd
}
actual fun splitToSequence(input: CharSequence, limit: Int = 0): Sequence<String>(source)

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

The last element of the resulting sequence corresponds to an input subsequence starting right after the last match (or at the beginning of input char sequence if there were no matches) and ending at the end of input. That implies that if input does not contain subsequences matching this regular expression, the resulting sequence will contain a single element corresponding to the whole input sequence. It also implies that for char sequences ending with a this regular expression match, the resulting sequence will end with an empty string.

Since Kotlin

1.6

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.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   fun Sequence<String>.toPrettyString() = joinToString(", ", "[", "]")

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

val splitNonMatchingInput = regex.splitToSequence("Split_me_if_you_can").toPrettyString()
println(splitNonMatchingInput) // [Split_me_if_you_can]

val splitSurroundedByMatches = regex.splitToSequence(",(-.-),").toPrettyString()
println(splitSurroundedByMatches) // [, (-.-), ]

println("regex.splitToSequence(\"\").toList() == listOf(\"\") is ${regex.splitToSequence("").toList() == listOf("")}") // true 
   //sampleEnd
}
actual fun splitToSequence(input: CharSequence, limit: Int = 0): Sequence<String>(source)

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

The last element of the resulting sequence corresponds to an input subsequence starting right after the last match (or at the beginning of input char sequence if there were no matches) and ending at the end of input. That implies that if input does not contain subsequences matching this regular expression, the resulting sequence will contain a single element corresponding to the whole input sequence. It also implies that for char sequences ending with a this regular expression match, the resulting sequence will end with an empty string.

Since Kotlin

1.8

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.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   fun Sequence<String>.toPrettyString() = joinToString(", ", "[", "]")

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

val splitNonMatchingInput = regex.splitToSequence("Split_me_if_you_can").toPrettyString()
println(splitNonMatchingInput) // [Split_me_if_you_can]

val splitSurroundedByMatches = regex.splitToSequence(",(-.-),").toPrettyString()
println(splitSurroundedByMatches) // [, (-.-), ]

println("regex.splitToSequence(\"\").toList() == listOf(\"\") is ${regex.splitToSequence("").toList() == listOf("")}") // true 
   //sampleEnd
}
actual fun splitToSequence(input: CharSequence, limit: Int = 0): Sequence<String>(source)

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

The last element of the resulting sequence corresponds to an input subsequence starting right after the last match (or at the beginning of input char sequence if there were no matches) and ending at the end of input. That implies that if input does not contain subsequences matching this regular expression, the resulting sequence will contain a single element corresponding to the whole input sequence. It also implies that for char sequences ending with a this regular expression match, the resulting sequence will end with an empty string.

Since Kotlin

1.8

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.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   fun Sequence<String>.toPrettyString() = joinToString(", ", "[", "]")

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

val splitNonMatchingInput = regex.splitToSequence("Split_me_if_you_can").toPrettyString()
println(splitNonMatchingInput) // [Split_me_if_you_can]

val splitSurroundedByMatches = regex.splitToSequence(",(-.-),").toPrettyString()
println(splitSurroundedByMatches) // [, (-.-), ]

println("regex.splitToSequence(\"\").toList() == listOf(\"\") is ${regex.splitToSequence("").toList() == listOf("")}") // true 
   //sampleEnd
}