Snippet | List to String | Join operation & advance use-cases 🤖

android Aug 23, 2020

Small snippets on how you can transform list to a string.

Disclaimer : All the assets used as background belongs to respective companies I don't promote them as my own, they are just used to make code relatable and attractive.


List to string conversion is very useful in competitive programming these days, here I showcase joinToString() function and various operation that can be performed with it. Now then 👁️ on the code 💻👇

List to String?

Convert a list to string using join function.

//list input

val venomQuote = listOf("Eyes", "Lungs", "Pancreas", "So", "many","snacks","so","little","time")

// join list elements into one string?

venomQuote.joinToString()

// output : Eyes, Lungs, Pancreas, So, many, snacks, so, little, time
kotlin | join to string

List to String with Separator?

How to provide custom gluing symbol instead of  ,.

//list input

val venomQuote = listOf("Eyes", "Lungs", "Pancreas", "So", "many","snacks","so","little","time")

// join list elements into one string using a different separator?

venomQuote.joinToString(
	separator = " " // define custom seperator
)

// output : Eyes Lungs Pancreas So many snacks so little time
kotlin | join to string separator

List to String with Start and End symbol ?

How to add start (prefix) and end (suffix) to the string. Prefix and Suffix will not be affected by separator or any transformation(discussed later) performed while undergoing the operation.

//list input

val venomQuote = listOf("Eyes", "Lungs", "Pancreas", "So", "many","snacks","so","little","time")

// join list elements into one string with a start and end string?

venomQuote.joinToString(
	separator = " ",
    	prefix = "`", // define start symbol 
        postfix = "` - Venom" // define end symbol
)

// output : `Eyes Lungs Pancreas So many snacks so little time` - Venom
kotlin | join to string prefix suffix

List to String with truncating after certain limit ?

I want to show ... if text is very long.

//list input

val venomQuote = listOf("Eyes", "Lungs", "Pancreas", "So", "many","snacks","so","little","time")

// join list elements into one string with truncating after a limit?

venomQuote.joinToString(
	separator = " ",
    	prefix = "`",
        postfix = "` - Venom",
        limit = 6 // define truncate limit (inclusive)
)

// output : `Eyes Lungs Pancreas So many snacks ...` - Venom
kotlin | join to string truncate

List to String with custom truncate symbol ?

Can I provide anything else than ...?

//list input

val venomQuote = listOf("Eyes", "Lungs", "Pancreas", "So", "many","snacks","so","little","time")

// join list elements into one string and truncating with different symbol?

venomQuote.joinToString(
    separator = " ",
    prefix = "`",
    postfix = "` - Venom",
    limit = 6,
    truncated = "!" // define truncate symbol
)

//  output : `Eyes Lungs Pancreas So many snacks !` - Venom
kotlin | join to string truncate modifier

List to String with some transformation per step ?

How to perform action/transformation per string concatenation..

//list input

val venomQuote = listOf("Eyes", "Lungs", "Pancreas", "So", "many","snacks","so","little","time")

// join list elements into one string and perform some action on each while merging?

venomQuote.joinToString(
    separator = " ",
    prefix = "`",
    postfix = "` - Venom",
    limit = 6,
    truncated = "!"){ "☠️ $it ☠️" // do stuff...}
    
// output : `☠️ Eyes ☠️ ☠️ Lungs ☠️ ☠️ Pancreas ☠️ ☠️ So ☠️ ☠️ many ☠️ ☠️ snacks ☠️ !` - Venom
kotlin | join to string transformations

That's all fokes! , these are the basic operations that can be performed on the join function and are always handy to know! see ya in next snippet 💻 Happy Hacking!


Enjoy the article?

a clap is much appreciated if you enjoyed. No sign up or cost associated :)


P.S Oh.. before I forget! List is supported only because its Iterable, discover more about it on official docs :

joinToString - Kotlin Programming Language



Chetan gupta

Hi there! call me Ch8n, I'm a mobile technology enthusiast! love Android #kotlinAlltheWay, CodingMantra: #cleanCoder #TDD #SOLID #designpatterns

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.