Snippet | List to String | Join operation & advance use-cases 🤖
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
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
Enjoying the Post?
a clap is much appreciated if you enjoyed. No sign up or cost associated :)
If you want to support my content do consider dropping a tip/coffee/donation💰
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
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
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
Enjoying the Post?
a clap is much appreciated if you enjoyed. No sign up or cost associated :)
If you want to support my content do consider dropping a tip/coffee/donation💰
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
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!
Enjoying the Post?
a clap is much appreciated if you enjoyed. No sign up or cost associated :)
If you want to support my content do consider dropping a tip/coffee/donation💰
P.S Oh.. before I forget! List is supported only because its Iterable
, discover more about it on official docs :
