This article explores different ways to split a comma-separated String into a List in Kotlin.

Kotlin doesn’t provide any built-in function to convert a String to a List. We have the split() function, but that split a string into an array. The idea is to call the split() function on the string using the regex \s*,\s* as a delimiter, and convert the resultant string array into a list. The regex \s*,\s* matches with a comma, preceded/followed by zero or more whitespace characters. Here’s the complete code:

Download Code

Output:

[a, b, c, d]

 
If we need the mutable instance of the list, consider using the mutableListOf() function over the listOf() function.

Download Code

Output:

[a, b, c, d]

 
We can directly call the toMutableList() function on the resultant array with the split() function.

Download Code

Output:

[a, b, c, d]

That’s all about splitting a String into a List in Kotlin.