Mastering Kotlin: A Collection of Essential Functions Every Developer Should Know with Examples — — -Last Part-String Representation

Hossein Ameri
2 min readNov 2, 2023

--

If you are generating user-visible text out of Kotlin collections or trying to log an internal state you will need to represent collections as strings. I really liked when I learned that Kotlin standard library has a function just for this reason:

.joinToString()

If don’t pass any parameters to .joinToString(), string representations of each items will be concatenated in a string and separated by commas with spaces by default.

Customizing the separator, prefix and postfix strings

However, if you need to change the separator strings or add some more useful information to the start or to the end you can pass some arguments for separator, prefix and postfix parameters.

The limit and truncated arguments

.joinToString() also support limiting and truncating the list. If you have a long list and don’t want to print everything and make a mess, you can limit how many elements you want in the resulting string and what string to use in the place of an ellipsis.

Finally, it also supports passing a transform lambda if you also want to define how each item will be converted into a string.

That was it! Now you have learned how to transform Kotlin collections using a handful of convenience functions.

--

--

Hossein Ameri

"Tech enthusiast and Android aficionado. Sharing insights and tips on all things Android. Passionate about mobile innovation and user experience."