This article explores different ways to append strings separated by a comma in Kotlin.

There are several ways to convert a collection to a comma-separated String in Kotlin:

1. Using joinToString() function

The standard solution to join elements of a list with the specified delimiter is using the joinToString() function. Its usage is demonstrated below:

Download Code

 
You can customize the output to our style as follows:

Download Code

2. Using toString() function

The toString() function returns the string representation of the collection, which consists of all its elements, enclosed in square brackets [], and adjacent elements separated by the delimiter ", ".

Download Code

3. Using String.join() function

Another possibility to join strings together with a specified separator is with the java.lang.String.join() function, which only works for an Iterable<String>

Download Code

4. Using String builder

Finally, you can create a utility function to traverse the list and append each element to a String builder with the specified delimiter. This logic would translate to the following code:

Download Code

That’s all about appending strings separated by a comma in Kotlin.