Kotlin Split String with Delimiters and Regular Expressions

Kotlin provides the String.split() function to divide a string into a list of substrings. You can split text using one delimiter, multiple delimiters, or a regular expression. The function also supports case-insensitive matching and a limit on the number of resulting parts.

A delimiter is a character or string that marks the boundary between values. For example, a comma is the delimiter in red,green,blue.

Kotlin split() Return Type and Basic Syntax

The split() function returns a List<String>. It does not modify the original string.

</>
Copy
string.split(vararg delimiters: String, ignoreCase: Boolean = false, limit: Int = 0)

The delimiters specify where the string should be divided. Set ignoreCase to true for case-insensitive delimiter matching. Use limit to restrict the number of returned substrings.

Example 1 – Split a Kotlin String Using One Delimiter

In the following example, the string Kotlin TutorialsepTutorial KartsepExamples is split wherever the delimiter sep occurs.

example.kt

fun main(args: Array<String>) {

    var str = "Kotlin TutorialsepTutorial KartsepExamples"
    var delimiter = "sep"

    val parts = str.split(delimiter)

    print(parts)
}

Output

[Kotlin Tutorial, Tutorial Kart, Examples]

Each occurrence of sep is removed from the result and becomes a boundary between two list elements.

Split a Comma-Separated String in Kotlin

A common use of split() is separating comma-delimited values. Use trim() on each result when spaces may appear after the commas.

</>
Copy
fun main() {
    val colors = "red, green, blue"
    val parts = colors.split(",").map { it.trim() }

    println(parts)
}

Output

[red, green, blue]

Example 2 – Split a Kotlin String Using Multiple Delimiters

You can pass multiple delimiters as arguments to split(). The string is divided whenever any supplied delimiter matches.

</>
Copy
String.split(delimiter1, delimiter2, .., delimiterN)

In the following example, the string Kotlin TutorialsepTutorialasepKartsepExamples is split using sep and asep.

example.kt

fun main(args: Array<String>) {

    var str = "Kotlin TutorialsepTutorialasepKartsepExamples"
    var delimiter1 = "sep"
    var delimiter2 = "asep"

    val parts = str.split(delimiter1, delimiter2)

    print(parts)
}

Output

[Kotlin Tutorial, Tutorial, Kart, Examples]

When delimiters overlap, their order can affect which delimiter is matched. Put a longer, more specific delimiter before a shorter delimiter when both may start at the same position.

Example 3 – Split a Kotlin String While Ignoring Case

The string-delimiter overload of split() accepts the ignoreCase option. Its default value is false, so delimiter matching is case-sensitive unless you explicitly enable case-insensitive matching.

String.split(vararg delimiters, ignoreCase:Boolean = false)

Pass ignoreCase = true as a named argument when uppercase and lowercase forms of a delimiter should be treated as equivalent.

In the following example, the string Kotlin TutorialsEPTutorialaSEpKartSEpExamples is split using the delimiters SEP and ASEP, regardless of letter case.

example.kt

fun main(args: Array<String>) {

    var str = "Kotlin TutorialsEPTutorialaSEpKartSEpExamples"
    var delimiter1 = "SEP"
    var delimiter2 = "ASEP"

    val parts = str.split(delimiter1, delimiter2, ignoreCase = true)

    print(parts)
}

Output

[Kotlin Tutorial, Tutorial, Kart, Examples]

Example 4 – Split a Kotlin String Using a Regular Expression

Use the Regex overload when the separator follows a pattern instead of being one fixed string. In this example, the alternation operator | allows either sep or asep to match.

example.kt

fun main(args: Array<String>) {

    var str = "Kotlin TutorialsepTutorialasepKartsepExamples"

    val parts = str.split(Regex("sep|asep"))

    print(parts)
}

Output

[Kotlin Tutorial, Tutorial, Kart, Examples]

Split a Kotlin String on Variable Whitespace

A whitespace regular expression is useful when words may be separated by spaces, tabs, or multiple consecutive whitespace characters.

</>
Copy
fun main() {
    val text = "Kotlin   split\tstring"
    val words = text.split(Regex("\\s+"))

    println(words)
}

Output

[Kotlin, split, string]

If the input may begin or end with whitespace, call trim() before splitting to avoid empty elements at the boundaries.

Limit the Number of Parts Returned by Kotlin split()

The limit argument sets the maximum number of substrings in the returned list. Any remaining delimiters stay in the final element.

</>
Copy
fun main() {
    val record = "101:Kotlin:Beginner:Active"
    val parts = record.split(":", limit = 3)

    println(parts)
}

Output

[101, Kotlin, Beginner:Active]

This is useful when only the first few fields should be separated and the remainder should be kept intact.

Handle Empty Values After Splitting a Kotlin String

Consecutive delimiters can produce empty strings in the returned list. You can retain those values when they represent missing fields, or remove them with filter().

</>
Copy
fun main() {
    val data = "Kotlin,,Java,"

    val allParts = data.split(",")
    val nonEmptyParts = allParts.filter { it.isNotEmpty() }

    println(allParts)
    println(nonEmptyParts)
}

Output

[Kotlin, , Java, ]
[Kotlin, Java]

Do not remove empty elements automatically when their positions carry meaning, such as an omitted value in a delimited record.

Split a Kotlin String into Characters

To obtain individual characters, use toList() when a List<Char> is suitable. Use map { it.toString() } when the result must contain one-character strings.

</>
Copy
fun main() {
    val text = "Kotlin"

    val characters: List<Char> = text.toList()
    val characterStrings: List<String> = text.map { it.toString() }

    println(characters)
    println(characterStrings)
}

Output

[K, o, t, l, i, n]
[K, o, t, l, i, n]

Split a Kotlin String at a Specific Index

An index is not a delimiter, so substring() or take() and drop() are clearer choices. The following example divides a string before index 6.

</>
Copy
fun main() {
    val text = "KotlinTutorial"
    val index = 6

    val first = text.substring(0, index)
    val second = text.substring(index)

    println(first)
    println(second)
}

Output

Kotlin
Tutorial

The index must be between zero and the string length. Validate a dynamic index before calling substring() to avoid an index-out-of-bounds exception.

Kotlin String split() FAQs

What does split() return in Kotlin?

split() returns a List<String>. Each list element contains the text found between matching delimiters.

How do I split a Kotlin string by multiple spaces?

Trim the string and split it with Regex("\\s+"). The \s+ pattern matches one or more whitespace characters, including spaces and tabs.

How do I remove empty strings from a split result?

Call filter { it.isNotEmpty() } on the returned list. If elements may contain only whitespace, use filter { it.isNotBlank() }.

Should I use split() or substring() to divide text at an index?

Use split() when a delimiter or pattern identifies the boundaries. Use substring(), take(), or drop() when the boundary is a character index.

Does Kotlin split() change the original string?

No. Kotlin strings are immutable. The function returns a new list and leaves the source string unchanged.

Summary of Kotlin String Splitting

In this Kotlin Tutorial, we learned how to split a string using one or more delimiters, case-insensitive matching, regular expressions, and a result limit. We also covered comma-separated values, variable whitespace, empty elements, character conversion, and splitting at an index.