Kotlin Array - joinToString() Function



The Kotlin array joinToString() function creates a string from an array (or any collection) with a specified separator, prefix, suffix, and transform function.

This function allows for wide customization 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 joinToString() function −

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

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 joinToString() function −

fun main(args: Array<String>) {
   val numbers = arrayOf(1, 2, 3, 4, 5)
   println(numbers.joinToString())
}

Output

Following is the output −

1, 2, 3, 4, 5

Example 2

Following is the another example. Here, we use joinToString function with the parameters: separator, prefix, and postfix −

fun main(args: Array<String>) {
    val numbers = listOf(1, 2, 3, 4, 5, 6)
    //scenario second
    println(numbers.joinToString(prefix = "{ ", postfix = " }", separator = " < "))
}

Output

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

{ 1 < 2 < 3 < 4 < 5 < 6 }

Example 3

The example below building a string and appending the number into the string. We then use the joinToString() function to convert number into string −

fun main(args: Array<String>) {
   val sb = StringBuilder("tutorials point: ")
   sb.append("India\n")

   val lotOfNumbers: Iterable<Int> = 1..100
   sb.append("First 8 numbers: ")

   // Use joinToString and appends its result to the StringBuilder
   val numbersString = lotOfNumbers.joinToString(limit = 8, separator = ":")
   sb.append(numbersString)

   println(sb.toString())
}

Output

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

tutorials point: India
First 8 numbers: 1:2:3:4:5:6:7:8:...
kotlin_arrays.htm
Advertisements