To concatenate strings in a list in Kotlin, you can use the `joinToString` method.
In this example,
words
with some strings.joinToString
method to concatenate the strings in the list with a specified separator.fun main() {
val words = listOf("Hello", "World", "!")
val concatenated = words.joinToString(separator = " ")
println("Concatenated string: $concatenated")
}
Concatenated string: Hello World !
In this example,
phrases
with different strings.joinToString
method to concatenate the strings in this list with a specified separator.fun main() {
val phrases = listOf("Good", "morning", "everyone")
val concatenated = phrases.joinToString(separator = ", ")
println("Concatenated string: $concatenated")
}
Concatenated string: Good, morning, everyone
In this tutorial, we learned How to Concatenate Strings in List in Kotlin language with well detailed examples.