Kotlin String split()

Kotlin String split() Tutorial

The String.split() function in Kotlin is used to split a string into an array of substrings based on a specified delimiter. It allows you to break a string into parts using a delimiter and returns an array of substrings as a result.

In this tutorial, we’ll explore the syntax of the split() function and provide examples of its usage in Kotlin strings.

Syntax

The syntax of the split() function is as follows:

fun CharSequence.split(
    vararg delimiters: String,
    ignoreCase: Boolean = false,
    limit: Int = 0
): List<String>

where

ParameterDescription
delimiterThe string used as the delimiter for splitting the original string.
ignoreCaseIf true, the case is ignored while matching the delimiter in the string. If false, the case of characters is considered while matching the delimiter in the string.
limitAn optional parameter specifying the maximum number of substrings to be returned. If set to a positive value, at most limit - 1 splits are performed.
Parameters of String.split() function

The delimiter parameter represents the string used as the delimiter for splitting the original string. The limit parameter is optional and determines the maximum number of substrings to be returned. If limit is set to a positive value, at most limit - 1 splits are performed.

Examples for String split() function

1. Split given string by Space

In this example, we’ll use split() to split a string into an array of substrings using a space as the delimiter.

  1. Take a string value in originalString.
  2. Call split() function on originalString with a space as the delimiter. The function returns an array of substrings.
  3. You may print the original string and the resulting array of substrings to the console output.

Kotlin Program

fun main() {
    val originalString = "Kotlin is awesome!"

    // Using split() to split the string by space
    val resultArray = originalString.split(" ")

    // Printing the original string and the resulting array of substrings
    println("Original String:\n$originalString\n")
    println("Result Array:\n$resultArray")
}

Output

Original String:
Kotlin is awesome!

Result Array:
[Kotlin, is, awesome!]

2. Split String by Comma with Limit

In this example, we’ll use split() to split a string into an array of substrings using a comma as the delimiter with a specified limit.

  1. Take a string value in originalString.
  2. Call split() function on originalString with a comma as the delimiter and a limit of 3. The function returns an array of substrings with at most three elements.
  3. You may print the original string and the resulting array of substrings to the console output.

Kotlin Program

fun main() {
    val originalString = "apple,orange,banana,grape"

    // Using split() to split the string by comma with a limit of 3
    val resultArray = originalString.split(",", limit = 3)

    // Printing the original string and the resulting array of substrings
    println("Original String:\n$originalString\n")
    println("Result Array:\n$resultArray")
}

Output

Original String:
apple,orange,banana,grape

Result Array:
[apple, orange, banana,grape]

Summary

In this tutorial, we’ve covered the split() function in Kotlin strings, its syntax, and how to use it to split a string into an array of substrings based on a specified delimiter. This function is useful for tasks such as parsing data and extracting components from strings.