This article explores different ways to construct a new string from elements of a collection separated by the specified delimiter in Kotlin.

1. Using StringBuilder class

The idea is to iterate over the given collection and build a string out of each encountered element separated with the delimiter using a StringBuilder.

Download Code

2. Using StringJoiner class

Alternatively, you can use the StringJoiner class, whose constructor accepts a separator. To append elements to it, you can use the add() function.

Download Code

3. Using String.join() function

Another plausible way is to use the join() function provided by the StringJoiner class, which takes a delimiter and joins elements of the specified collection together with it.

Download Code

That’s all about merging elements of a Collection separated by a delimiter in Kotlin.