This article explores different ways to split a string into an array using a given delimiter in Kotlin.

1. Using String’s split() function

The standard solution to split a string in Kotlin is with the native split() function, which takes one or more delimiters as an argument and splits the string around occurrences of the specified delimiters.

Download Code

 
The split() function returns a list of strings. If you want to convert the list into an array, call the toTypedArray() function.

Download Code

2. Using Pattern’s split() function

You can also use Pattern’s split() function, which returns an array of strings computed by splitting the input around matches of the given pattern.

Download Code

 
If your delimiter happens to be the same as special characters in regex like a dot(.), pipe(|) or dollar($), you need to escape them first.

Download Code

That’s all about splitting a string using a delimiter in Kotlin.