This article explores different ways to convert a comma-separated string to a list in Kotlin.

The standard way to split a Kotlin string is with the split() function. We can use the split() function to convert the comma-separated string to a list in the following ways:

1. Using listOf() function

The idea is to split the string with the split() function and pass the resultant array to the listOf function to get a new list.

Download Code

2. Using toList() function

Another good solution is to directly call the toList() function after splitting the array with the split() function.

Download Code

3. Using Pattern.compile() function

Finally, you can split the string using the split() function around the matches of a pattern. This would translate to a simple code below:

Download Code

That’s all about converting a comma-separated string to a list in Kotlin.