How to split a string in Kotlin:
In this post, we will learn different ways to split a string in Kotlin. Kotlin provides an inbuilt method to split a string. We can use a delimeter or a regular expression to split a string. Let’s learn how to split a string in different ways in Kotlin.
Definition of split:
Kotlin provides an inbuilt method split to split a string. We can pass a character or a string as its delimeter. It returns a list of strings, i.e. the split result.
It is defined as:
str.split(vararg delimiters: Char, ignoreCase: Boolean = false, limit: Int = 0): List<String>
str.split(vararg delimiters: String, ignoreCase: Boolean = false, limit: Int = 0): List<String>
Here,
- delimiters is one or more strings or characters to use as the delimiter.
- ignoreCase is used to define if we are ignoring the character case or not while matching the delimiters. This is an optional value and it is false by default.
- limit defines the maximum number of substrings to return by this method. This is also an optional value and it is 0 by default i.e. no limit is set for the split operation.
We can also pass a regular expression to the split method.
str.split(regex: Regex, limit: Int = 0): List<String>
It will split around matches of the regular expression regex. The second parameter limit is the maximum number of substrings to return by this method.
Example 1: Split a string by passing a single delimiter:
Let’s try to split a string by passing a single delimiter:
fun main() {
val givenString = "one-two-three-four-five"
val splittedString = givenString.split('-')
println(splittedString)
}
We are passing the character - as the delimiter. It will print:
[one, two, three, four, five]
Similarly, let’s try it with a single string delimiter:
fun main() {
val givenString = "one--two--three--four--five"
val splittedString = givenString.split("--")
println(splittedString)
}
It will print the same result:
[one, two, three, four, five]
Example 2: Split a string by passing multiple delimiters:
We can also pass multiple delimiters to the split function. It will split the string at any of these characters or strings:
fun main() {
val givenString = "one-two/three?four=five"
val splittedString = givenString.split('-', '/', '?', '=')
println(splittedString)
}
This will split the givenString at any of -, /, ? or =. If you run this program, it will print:
[one, two, three, four, five]
Example 3: Split a string by ignoring character case:
We can pass ignoreCase as true to ignore the character case for the split operation. Let’s take a look at the below example:
fun main() {
val givenString = "1break234Break567BREAK890bReaK11"
val splittedString = givenString.split("break", ignoreCase = true)
println(splittedString)
}
It splits the string at break by ignoring the character case of the word. It will print:
[1, 234, 567, 890, 11]
Example 4: Split a string with limit:
We can pass a limit value to specify the total number of substrings to return. For example:
fun main() {
val givenString = "one-two-three-four-five-six"
val splittedString = givenString.split('-', limit = 3)
println(splittedString)
}
The limit is 3, so it will return three substrings:
[one, two, three-four-five-six]
Example 5: Split a string with a regular expression:
The following example will split a string with a regular expression:
fun main() {
val givenString = "one1two2three3four4five5six6seven"
val splittedString = givenString.split(Regex("\\d"))
println(splittedString)
}
It is splilt in any number as the regular expression is matching any number value. It will match for 1, 2, 3, 4, 5, 6, 7, 8, or 9.
Output:
[one, two, three, four, five, six, seven]
You might also like:
- 5 ways to swap two numbers in Kotlin
- 2 ways to find the average of array numbers in kotlin
- How to remove specific elements from an array using index in Kotlin
- Kotlin program to create a simple calculator
- How to remove the first n characters from a string in Kotlin
- Check if a string starts with a character or sub-string in Kotlin
- Check if a string ends with another string or character in Kotlin
- How to compare two strings in Kotlin in different ways