Kotlin Set joinTo()
Syntax & Examples


Set.joinTo() extension function

The joinTo() extension function in Kotlin appends the string representation of all the elements in the set, separated using the specified separator, and using the given prefix and postfix if supplied.


Syntax of Set.joinTo()

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

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

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

Parameters

ParameterOptional/RequiredDescription
bufferrequiredThe Appendable to which the elements will be appended.
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.joinTo() returns value of type A.



✐ Examples

1 Joining integers into a string with a comma separator

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

For example,

  1. Create a set of integers.
  2. Create a StringBuilder to serve as the buffer.
  3. Use joinTo() to join the elements into the buffer with a comma separator.
  4. Print the resulting string.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3)
    val buffer = StringBuilder()
    numbers.joinTo(buffer, separator = ", ")
    println(buffer.toString())
}

Output

1, 2, 3

2 Joining strings with a prefix and postfix

Using joinTo() 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. Create a StringBuilder to serve as the buffer.
  3. Use joinTo() to join the elements into the buffer with a prefix and postfix.
  4. Print the resulting string.

Kotlin Program

fun main() {
    val strings = setOf("a", "b", "c")
    val buffer = StringBuilder()
    strings.joinTo(buffer, prefix = "[", postfix = "]")
    println(buffer.toString())
}

Output

[a, b, c]

3 Joining integers with a limit and truncated string

Using joinTo() 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. Create a StringBuilder to serve as the buffer.
  3. Use joinTo() to join the elements into the buffer with a limit and truncated string.
  4. Print the resulting string.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4, 5)
    val buffer = StringBuilder()
    numbers.joinTo(buffer, limit = 3, truncated = "...")
    println(buffer.toString())
}

Output

1, 2, 3...

Summary

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