split
Splits the input CharSequence to a list of strings around matches of this regular expression.
The last element of the resulting list 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 list 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 list will end with an empty string.
Since Kotlin
1.0Parameters
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
val colors = "green, red , brown&blue, orange, pink&green"
val regex = "[,\\s]+".toRegex()
val mixedColor = regex.split(colors)
.onEach { println(it) }
.firstOrNull { it.contains('&') }
println(mixedColor) // brown&blue
val splitNonMatchingInput = regex.split("Split_me_if_you_can")
println(splitNonMatchingInput) // [Split_me_if_you_can]
val splitSurroundedByMatches = regex.split(",(-.-),")
println(splitSurroundedByMatches) // [, (-.-), ]
println("regex.split(\"\") == listOf(\"\") is ${regex.split("") == listOf("")}") // true
//sampleEnd
}Splits the input CharSequence to a list of strings around matches of this regular expression.
The last element of the resulting list 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 list 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 list will end with an empty string.
Since Kotlin
1.1Parameters
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
val colors = "green, red , brown&blue, orange, pink&green"
val regex = "[,\\s]+".toRegex()
val mixedColor = regex.split(colors)
.onEach { println(it) }
.firstOrNull { it.contains('&') }
println(mixedColor) // brown&blue
val splitNonMatchingInput = regex.split("Split_me_if_you_can")
println(splitNonMatchingInput) // [Split_me_if_you_can]
val splitSurroundedByMatches = regex.split(",(-.-),")
println(splitSurroundedByMatches) // [, (-.-), ]
println("regex.split(\"\") == listOf(\"\") is ${regex.split("") == listOf("")}") // true
//sampleEnd
}Splits the input CharSequence to a list of strings around matches of this regular expression.
The last element of the resulting list 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 list 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 list will end with an empty string.
Since Kotlin
1.0Parameters
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
val colors = "green, red , brown&blue, orange, pink&green"
val regex = "[,\\s]+".toRegex()
val mixedColor = regex.split(colors)
.onEach { println(it) }
.firstOrNull { it.contains('&') }
println(mixedColor) // brown&blue
val splitNonMatchingInput = regex.split("Split_me_if_you_can")
println(splitNonMatchingInput) // [Split_me_if_you_can]
val splitSurroundedByMatches = regex.split(",(-.-),")
println(splitSurroundedByMatches) // [, (-.-), ]
println("regex.split(\"\") == listOf(\"\") is ${regex.split("") == listOf("")}") // true
//sampleEnd
}Splits the input CharSequence to a list of strings around matches of this regular expression.
The last element of the resulting list 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 list 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 list will end with an empty string.
Since Kotlin
1.3Parameters
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
val colors = "green, red , brown&blue, orange, pink&green"
val regex = "[,\\s]+".toRegex()
val mixedColor = regex.split(colors)
.onEach { println(it) }
.firstOrNull { it.contains('&') }
println(mixedColor) // brown&blue
val splitNonMatchingInput = regex.split("Split_me_if_you_can")
println(splitNonMatchingInput) // [Split_me_if_you_can]
val splitSurroundedByMatches = regex.split(",(-.-),")
println(splitSurroundedByMatches) // [, (-.-), ]
println("regex.split(\"\") == listOf(\"\") is ${regex.split("") == listOf("")}") // true
//sampleEnd
}Splits the input CharSequence to a list of strings around matches of this regular expression.
The last element of the resulting list 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 list 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 list will end with an empty string.
Since Kotlin
1.8Parameters
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
val colors = "green, red , brown&blue, orange, pink&green"
val regex = "[,\\s]+".toRegex()
val mixedColor = regex.split(colors)
.onEach { println(it) }
.firstOrNull { it.contains('&') }
println(mixedColor) // brown&blue
val splitNonMatchingInput = regex.split("Split_me_if_you_can")
println(splitNonMatchingInput) // [Split_me_if_you_can]
val splitSurroundedByMatches = regex.split(",(-.-),")
println(splitSurroundedByMatches) // [, (-.-), ]
println("regex.split(\"\") == listOf(\"\") is ${regex.split("") == listOf("")}") // true
//sampleEnd
}Splits the input CharSequence to a list of strings around matches of this regular expression.
The last element of the resulting list 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 list 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 list will end with an empty string.
Since Kotlin
1.8Parameters
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
val colors = "green, red , brown&blue, orange, pink&green"
val regex = "[,\\s]+".toRegex()
val mixedColor = regex.split(colors)
.onEach { println(it) }
.firstOrNull { it.contains('&') }
println(mixedColor) // brown&blue
val splitNonMatchingInput = regex.split("Split_me_if_you_can")
println(splitNonMatchingInput) // [Split_me_if_you_can]
val splitSurroundedByMatches = regex.split(",(-.-),")
println(splitSurroundedByMatches) // [, (-.-), ]
println("regex.split(\"\") == listOf(\"\") is ${regex.split("") == listOf("")}") // true
//sampleEnd
}