split
Splits this char sequence to a list of strings around occurrences of the specified delimiters.
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.
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 matches at each position the first element in delimiters that is equal to a delimiter in this instance at that position.
Samples
import java.util.Locale
import java.util.regex.Pattern
import kotlin.test.*
fun main() {
//sampleStart
val multiCharDelimiter = "apple--banana--cherry".split("--")
println(multiCharDelimiter) // [apple, banana, cherry]
val multipleSplit = "apple->banana;;cherry:::orange".split("->", ";;", ":::")
println(multipleSplit) // [apple, banana, cherry, orange]
val longerDelimiterFirst = "apple<-banana<--cherry".split("<--", "<-")
println(longerDelimiterFirst) // [apple, banana, cherry]
val shorterDelimiterFirst = "apple<-banana<--cherry".split("<-", "<--")
println(shorterDelimiterFirst) // [apple, banana, -cherry]
val limitSplit = "a->b->c->d->e".split("->", limit = 3)
println(limitSplit) // [a, b, c->d->e]
val emptyInputResult = "".split("sep")
println("emptyInputResult == listOf(\"\") is ${emptyInputResult == listOf("")}") // true
val emptyDelimiterSplit = "abc".split("")
println(emptyDelimiterSplit) // [, a, b, c, ]
val mixedCase = "abcXYZdef".split("xyz")
println(mixedCase) // [abcXYZdef] // No match with case sensitivity
val mixedCaseIgnored = "abcXYZdef".split("xyz", ignoreCase = true)
println(mixedCaseIgnored) // [abc, def] // Matches with case insensitivity
val emptyResults = "##a##b##c##".split("##")
println(emptyResults) // [, a, b, c, ]
val consecutiveSeparators = "a--b------c".split("--")
println(consecutiveSeparators) // [a, b, , , c]
//sampleEnd
}
Splits this char sequence to a list of strings around occurrences of the specified delimiters.
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 java.util.Locale
import java.util.regex.Pattern
import kotlin.test.*
fun main() {
//sampleStart
val commaSplit = "apple,banana,cherry".split(',')
println(commaSplit) // [apple, banana, cherry]
val charSplit = "apple,banana;cherry".split(',', ';')
println(charSplit) // [apple, banana, cherry]
val limitSplit = "a,b,c,d,e".split(',', limit = 3)
println(limitSplit) // [a, b, c,d,e]
val emptyInputResult = "".split('|')
println("emptyInputResult == listOf(\"\") is ${emptyInputResult == listOf("")}") // true
val mixedCase = "abcXdef".split('x')
println(mixedCase) // [abcXdef] // No match with case sensitivity
val mixedCaseIgnored = "abcXdef".split('x', ignoreCase = true)
println(mixedCaseIgnored) // [abc, def] // Matches with case insensitivity
val emptyResults = ",a,b,c,".split(',')
println(emptyResults) // [, a, b, c, ]
val consecutiveSeparators = "a,,b,,,c".split(',')
println(consecutiveSeparators) // [a, , b, , , c]
//sampleEnd
}
Splits this char sequence to a list of strings around matches of the given regular expression.
Since Kotlin
1.0Parameters
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 java.util.regex.Pattern
import kotlin.test.*
fun main() {
//sampleStart
val digitSplit = "apple123banana456cherry".split(Regex("\\d+"))
println(digitSplit) // [apple, banana, cherry]
val wordBoundarySplit = "The quick brown fox".split(Regex("\\s+"))
println(wordBoundarySplit) // [The, quick, brown, fox]
val limitSplit = "a,b,c,d,e".split(Regex(","), limit = 3)
println(limitSplit) // [a, b, c,d,e]
val patternGroups = "abc-123def_456ghi".split(Regex("[\\-_]\\d+"))
println(patternGroups) // [abc, def, ghi]
val caseInsensitiveSplit = "Apple123Banana45CHERRY".split(Regex("[a-z]+", RegexOption.IGNORE_CASE))
println(caseInsensitiveSplit) // [, 123, 45, ]
val emptyInputResult = "".split(Regex("sep"))
println("emptyInputResult == listOf(\"\") is ${emptyInputResult == listOf("")}") // true
val emptyDelimiterSplit = "abc".split(Regex(""))
println(emptyDelimiterSplit) // [, a, b, c, ]
val splitByMultipleSpaces = "a b c".split(Regex("\\s+"))
println(splitByMultipleSpaces) // [a, b, c]
val splitBySingleSpace = "a b c".split(Regex("\\s"))
println(splitBySingleSpace) // [a, , b, , , , c]
//sampleEnd
}
Splits this char sequence around matches of the given regular expression.
This function has two notable differences from the method Pattern.split:
the function returns the result as a
List<String>
rather than anArray<String>
;when the limit is not specified or specified as 0, this function doesn't drop trailing empty strings from the result.
Since Kotlin
1.0Parameters
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 java.util.regex.Pattern
import kotlin.test.*
fun main() {
//sampleStart
val digitSplit = "apple123banana456cherry".split(Pattern.compile("\\d+"))
println(digitSplit) // [apple, banana, cherry]
val wordBoundarySplit = "The quick brown fox".split(Pattern.compile("\\s+"))
println(wordBoundarySplit) // [The, quick, brown, fox]
val limitSplit = "a,b,c,d,e".split(Pattern.compile(","), limit = 3)
println(limitSplit) // [a, b, c,d,e]
val patternGroups = "abc-123def_456ghi".split(Pattern.compile("[\\-_]\\d+"))
println(patternGroups) // [abc, def, ghi]
val caseInsensitiveSplit = "Apple123Banana45CHERRY".split(Pattern.compile("[a-z]+", Pattern.CASE_INSENSITIVE))
println(caseInsensitiveSplit) // [, 123, 45, ]
val emptyInputResult = "".split(Pattern.compile("sep"))
println("emptyInputResult == listOf(\"\") is ${emptyInputResult == listOf("")}") // true
val emptyDelimiterSplit = "abc".split(Pattern.compile(""))
println(emptyDelimiterSplit) // [a, b, c, ]
val splitByMultipleSpaces = "a b c".split(Pattern.compile("\\s+"))
println(splitByMultipleSpaces) // [a, b, c]
val splitBySingleSpace = "a b c".split(Pattern.compile("\\s"))
println(splitBySingleSpace) // [a, , b, , , , c]
//sampleEnd
}