Kotlin Array.joinToString()

Kotlin Array.joinToString() function

In Kotlin, the Array.joinToString() function is used to create a single string from the elements of an array, with optional customizations for formatting. It is particularly useful when you want to concatenate array elements into a readable string representation.

In this tutorial, we’ll explore the syntax of the joinToString() function and provide examples of its usage in Kotlin arrays.

Syntax

The syntax of the Array.joinToString() function is as follows:

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

where

ParameterDescription
separatorThe separator between the elements. The default is “, “.
prefixThe string to be prefixed to the resulting string. The default is an empty string.
postfixThe string to be appended to the resulting string. The default is an empty string.
limitThe maximum number of elements to include in the result. If the number of elements exceeds this limit, the truncated string is appended. The default is -1, which means no limit.
truncatedThe string to be appended if the number of elements exceeds the specified limit. The default is “…”.
transformA function that transforms each element before joining. The default is null.
Parameters of Array.joinToString() function

The joinToString() function returns a string representation of the array elements based on the specified parameters.

Examples for Array.joinToString() function

1. Basic Usage of Array.joinToString()

In this example, we’ll use joinToString() to concatenate the elements of an array into a single string with the default settings.

Kotlin Program

fun main() {
    val fruitsArray = arrayOf("apple", "banana", "kiwi", "mango")

    // Using joinToString() with default settings
    val result = fruitsArray.joinToString()

    // Printing the original array and the result
    println("Fruits Array\n${fruitsArray.contentToString()}\n")
    println("Joined String\n$result")
}

Output

Fruits Array
[apple, banana, kiwi, mango]

Joined String
apple, banana, kiwi, mango

2. Customizing Separator, Prefix, and Postfix

In this example, we’ll customize the separator, prefix, and postfix used in the joined string.

Kotlin Program

fun main() {
    val fruitsArray = arrayOf("apple", "banana", "kiwi", "mango")

    // Using joinToString() with custom separator, prefix, and postfix
    val result = fruitsArray.joinToString(
        separator = " | ",
        prefix = "[",
        postfix = "]"
    )

    // Printing the original array and the result
    println("Fruits Array\n${fruitsArray.contentToString()}\n")
    println("Joined String\n$result")
}

Output

Fruits Array
[apple, banana, kiwi, mango]

Joined String
[apple | banana | kiwi | mango]

3. Limiting the Number of Elements

In this example, we’ll use joinToString() to limit the number of elements included in the result.

Kotlin Program

fun main() {
    val fruitsArray = arrayOf("apple", "banana", "kiwi", "mango")

    // Using joinToString() with a limit
    val result = fruitsArray.joinToString(
        separator = ", ",
        limit = 3,
        truncated = "..."
    )

    // Printing the original array and the result
    println("Fruits Array\n${fruitsArray.contentToString()}\n")
    println("Joined String\n$result")
}

Output

Fruits Array
[apple, banana, kiwi, mango]

Joined String
apple, banana, kiwi, ...

4. Transforming Elements Before Joining

In this example, we’ll use joinToString() with a transformation function to uppercase each element before joining.

Kotlin Program

fun main() {
    val fruitsArray = arrayOf("apple", "banana", "kiwi", "mango")

    // Using joinToString() with element transformation
    val result = fruitsArray.joinToString(
        transform = { it.uppercase() }
    )

    // Printing the original array and the result
    println("Fruits Array\n${fruitsArray.contentToString()}\n")
    println("Joined String\n$result")
}

Output

Fruits Array
[apple, banana, kiwi, mango]

Joined String
APPLE, BANANA, KIWI, MANGO

Summary

In this tutorial, we’ve covered the joinToString() function in Kotlin arrays, its syntax, and how to use it for creating customized string representations. Whether you need a simple concatenation or more complex formatting, joinToString() provides flexibility for joining array elements into a single string.