Kotlin Tutorials

Kotlin Set joinToString()
Syntax & Examples

Set.joinToString() extension function

The joinToString() extension function in Kotlin creates a string from all the elements in the set, separated using the specified separator, and using the given prefix and postfix if supplied.


Syntax of Set.joinToString()

The syntax of Set.joinToString() extension function is:

fun <T> Set<T>.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String

This joinToString() extension function of Set creates a string from all the elements separated using separator and using the given prefix and postfix if supplied.

Parameters

ParameterOptional/RequiredDescription
separatoroptionalThe separator used between elements. Default is ", ".
prefixoptionalThe prefix added before the first element. Default is an empty string.
postfixoptionalThe postfix added after the last element. Default is an empty string.
limitoptionalThe maximum number of elements to be appended. Default is -1, which means no limit.
truncatedoptionalThe string that will be appended if the limit is reached. Default is "...".
transformoptionalA function that transforms an element into a CharSequence which will be appended. Default is null.

Return Type

Set.joinToString() returns value of type String.



✐ Examples

1 Joining integers into a string with a comma separator

Using joinToString() to join elements in a set of integers into a string with a comma separator.

For example,

  1. Create a set of integers.
  2. Use joinToString() to join the elements into a string with a comma separator.
  3. Print the resulting string.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3)
    val result = numbers.joinToString(separator = ", ")
    println(result)
}

Output

1, 2, 3

2 Joining strings with a prefix and postfix

Using joinToString() to join elements in a set of strings into a string with a prefix and postfix.

For example,

  1. Create a set of strings.
  2. Use joinToString() to join the elements into a string with a prefix and postfix.
  3. Print the resulting string.

Kotlin Program

fun main() {
    val strings = setOf("a", "b", "c")
    val result = strings.joinToString(prefix = "[", postfix = "]")
    println(result)
}

Output

[a, b, c]

3 Joining integers with a limit and truncated string

Using joinToString() to join elements in a set of integers into a string with a limit and truncated string.

For example,

  1. Create a set of integers.
  2. Use joinToString() to join the elements into a string with a limit and truncated string.
  3. Print the resulting string.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4, 5)
    val result = numbers.joinToString(limit = 3, truncated = "...")
    println(result)
}

Output

1, 2, 3...

Summary

In this Kotlin tutorial, we learned about joinToString() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.