DEV Community

Cover image for Easy way to get a separated string from a list in Kotlin: joinToString()
Jess Barrientos
Jess Barrientos

Posted on • Edited on

Easy way to get a separated string from a list in Kotlin: joinToString()

joinToString() builds a single string from the collection elements based on the provided arguments if we decided to add them.

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

Ok, We can see that all arguments are optional, which means that we can use this function without setting any argument. In this case, the function will return the result similar to calling toString() on the collection: a String of elements.

listOf("1", "2", "3", "4", "5", "6").joinToString()
// output -> "1, 2, 3, 4, 5, 6"

listOf("1", "2", "3", "4", "5", "6").toString()
// output -> [1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

Now, let's change the value of each parameter.

Separator

This argument helps us add a separator for our string, the default value is ", " and we can change this value with whatever we want, as long as it's a CharSequence.

listOf("1", "2", "3", "4", "5", "6").joinToString(separator = ".")
// output -> "1.2.3.4.5.6"

listOf("1", "2", "3", "4", "5", "6").joinToString(separator = "-")
// output -> "1-2-3-4-5-6"

listOf("1", "2", "3", "4", "5", "6").joinToString(separator = "@#€")
// output -> "1@#€2@#€3@#€4@#€5@#€6"
Enter fullscreen mode Exit fullscreen mode

Prefix

It allows us to add an element to the beginning of the string.

listOf("1", "2", "3", "4", "5", "6").joinToString(prefix="$")
//output -> "$1, 2, 3, 4, 5, 6"

listOf("1", "2", "3", "4", "5", "6").joinToString(separator = "*", prefix="$")
//output -> "$1*2*3*4*5*6"
Enter fullscreen mode Exit fullscreen mode

Postfix

This adds an element to the end, as prefix, its default value is "", very useful when we need to create, for example, an SQL query or a regular expression.

listOf("1", "2", "3", "4", "5", "6").joinToString(postfix="$")
//output -> "1, 2, 3, 4, 5, 6$"

listOf("1", "2", "3", "4", "5", "6").joinToString(prefix = "[", postfix="]")
//output -> "[1, 2, 3, 4, 5, 6]"
Enter fullscreen mode Exit fullscreen mode

Limit

Sometimes, our collections could be huge, so we can tell the function that we only want to add a few elements. limit needs a non-negative value ( >= 0 ). By default, the value is "-1" which means there's no limit and all elements will be appended to the string, but if we set 0 nothing will be appended.

listOf("1", "2", "3", "4", "5", "6").joinToString(limit = 4)
// output -> "1, 2, 3, 4, ..."

listOf("1", "2", "3", "4", "5", "6").joinToString(limit = 0)
// output -> ...

listOf("1", "2", "3", "4", "5", "6").joinToString(limit = -1)
// output -> "1, 2, 3, 4, 5, 6"
Enter fullscreen mode Exit fullscreen mode

Truncated

As you can see when limit is added three dots appear at the end of the string, this happens because limit and truncated arguments go together. So by default, truncated value is "..." and we can use almost anything we want to customize it.

listOf("1", "2", "3", "4", "5", "6").joinToString(limit = 4, truncated = "")
// output -> "1, 2, 3, 4, "

listOf("1", "2", "3", "4", "5", "6").joinToString(limit = 4, truncated = "... will be continued")
// output -> "1, 2, 3, 4, ... will be continued"
Enter fullscreen mode Exit fullscreen mode

Transform

Finally, to customize the representation of elements themselves: transform. It helps us change each element before adding it to the result string, its default value is null.

listOf("1", "2", "3", "4", "5", "6").joinToString(transform = { it + 2 })
// output -> "12, 22, 32, 42, 52, 62"

listOf("1", "2", "3", "4", "5", "6").joinToString(transform = { it + " item" })
// output -> "1 item, 2 item, 3 item, 4 item, 5 item, 6 item"

listOf("1", "2", "3", "4", "5", "6").joinToString(transform = { "$"+it+".00" })

// output -> "$1.00, $2.00, $3.00, $4.00, $5.00, $6.00"
Enter fullscreen mode Exit fullscreen mode

To finish, I use this function when I need to create a SQL query or add a custom format, like the last code example adding it the "$" and ".00" to get a simple price format.

Now tell me, What other uses do you see it?


  • I have installed IntelliJ IDEA to run the examples.

Top comments (0)