This article explores different ways to convert a list to a string in Kotlin. All elements in the specified list should be joined into a single string separated by a delimiter. The solution should not add the delimiter before or after the string.

1. Using joinToString() function

The standard solution to join strings together with a specified delimiter is with the joinToString() function. This is demonstrated below:

Download Code

2. Using String.join() function

Another good solution is to use the join() function of Java’s String class. We can use this as:

Download Code

3. Using StringBuilder

Finally, you can use the StringBuilder to build the string character by character. We can do this by looping over the list and concatenate each character to the StringBuilder instance with the specified delimiter.

To deal with the trailing delimiters characters, you can use the removeSuffix() function before calling returning the StringBuilder as a string.

Download Code

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