Kotlin Array - joinTo() Function



The Kotlin array joinTo() function is used to convert an array (or any collection) to a string with a specified separator, prefix, suffix, and transform function.

This function allows for wide modification of how the elements are joined into a single string.

If the collection could be huge, you can specify a non-negative value for the limit parameter. In this case, only the first limit elements will be appended, followed by the truncated string (which defaults to "...").

Syntax

Following is the syntax of Kotlin array joinTo() function −

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

Parameters

This function accepts the following parameters, but not all of them are necessary.

  • buffer: It represent an Appendable where the result need to be appended.

  • separator: It represent a separator between elements. Default is ", ".

  • prefix: It represent a string that will be added before the first element. Default is an empty string "".

  • postfix: It represent a string that will be added after the last element. Default is an empty string "".

  • limit: It represent maximum number of elements to be appended. If more elements are available, the truncated string will be appended after the limit. Default is -1, meaning no limit.

  • truncated: It is a string to be appended when the limit is reached. Default is "...".

  • transform: It is a function that will be applied to each element to transform it before it is appended to the buffer.

Return value

This function returns a string containing the characters in this sequence in the same order as an array's or collection sequence.

Example 1

Following is the basic example to demonstrate the use of joinTo() function −

fun main(args: Array<String>) {
   val sb = StringBuilder("An existing string and an array: ")
   val numbers = arrayOf(1, 2, 3, 4, 5)
   println(numbers.joinTo(sb, prefix = "[", postfix = "]").toString())
}

Output

Following is the output −

An existing string and an array: [1, 2, 3, 4, 5]

Example 2

Following is the another example, here we are creating a string containing string and number from 1 to 100 using the joinTo function −

fun main(args: Array<String>) {
   val lotOfNumbers: Iterable<Int> = 1..100
   val sb = StringBuilder("First five numbers: ")
   println(lotOfNumbers.joinTo(sb,  limit = 7 ).toString())
}

Output

After execution of the above code we get the following output −

First five numbers: 1, 2, 3, 4, 5, 6, 7, ...

Example 3

The example below building a string, creating an iterable number and appending into the string. We then use the joinTo() function −

fun main(args: Array<String>) {
   val sb = java.lang.StringBuilder("This is tutorialspoint: ")
   val stringArray = arrayOf<String>("First", "Second", "Third")
   
   //first scenario
   println(stringArray.joinTo(sb, separator = "::" ))

   val lotOfNumbers: Iterable<Int> = 1..100
   val firstNumbers = StringBuilder("First 8 numbers: ")
   
   //second scenario
   println(lotOfNumbers.joinTo(firstNumbers,  limit = 8 ).toString())
}

Output

On execution of the above code we get the following output −

This is tutorialspoint: First::Second::Third
First 8 numbers: 1, 2, 3, 4, 5, 6, 7, 8, ...
kotlin_arrays.htm
Advertisements