Kotlin String - joinToString() Function



The Kotlin string joinToString() function is used to create a string representation by combining the elements of a collection with optional formatting features such as separators, prefixes, and postfixes.

The following are the uses of this function −

  • To convert a list or collections into a readable string with custom formatting.
  • It is useful for formatting data for output, such as for logs, UI, or debugging.
  • It summarizing large collections with truncation when exceeding a specified limit.

Syntax

Following is the syntax of the Kotlin string joinToString() function −

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

Parameters

This function accepts the following parameters −

  • separator: It specifies the string used to separate the collection's elements. Default is ", ".
  • prefix: A string to be appended to the beginning of the resulting string. Default is an empty string "".
  • postfix: A string to be appended to the end of the resulting string. Default is an empty string "".
  • limit: It specifies the number of elements that should be visible in the resulting string.
  • truncated: It specifies the string to append when the number of elements exceeds the limit. The default is "...".
  • transform: A lambda function that specifies how each element of a collection is transformed before being added to the resulting string. Default is null.

Return value

This function returns a string representing the concatenated element of a collection formatted by the given parameters.

Example 1: Join list element into a String

In the following example we use the default joinToString() function −

fun main() {
   val items = listOf("Apple", "Banana", "Cherry")
   val string = items.joinToString()
   println("This is string: " + string)
}

Output

Following is the output −

This is string: Apple, Banana, Cherry

Example 2

This is another example of the joinToString() function, in which we pass the separator, prefix, and postfix values −

fun main() {
   val items = listOf("The", "tutorialspoint", "India")
   val result = items.joinToString(separator = " | ", prefix = "[", postfix = "]")
   println(result)
}

Output

Following is the output −

[The | tutorialspoint | India]

Example 3: Limiting Elements with joinToString()

In this example, we limit the element by passing the value of limit to the joinToString() function −

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

Output

Following is the output −

1, 2, 3, and more

Example 4: Create Custom Transformation

The below example uses the transform parameter of the joinToString() function to create a custom transformation −

fun main() {
   val numbers = listOf(1, 2, 3)
   val result = numbers.joinToString(transform = { "Number: $it" })
   println(result)
}

Output

Following is the output −

Number: 1, Number: 2, Number: 3
kotlin_strings.htm
Advertisements