Kotlin – Split String by Comma

Split string by comma in Kotlin

To split a given string by comma delimiter in Kotlin, call split() function on the given string and pass the "," delimiter string as argument.

str.split(",")

The following is a simple use case where we take a string with values separated by comma character. And split the string by comma delimiter.

apple,banana,cherry,mango
     ,      ,      ,       <- delimiter positions
apple banana cherry mango  <- split values

Examples

Split string by comma separator

In the following program, we take a string in str and split this string into values where values in the string are separated by comma ",".

Kotlin Program

fun main() {
    val str = "apple,banana,cherry,mango"
    val values = str.split(",")
    for (value in values) {
        println(value)
    }
}

Output

apple
banana
cherry
mango