In this tutorial, you shall learn how to convert a given list of characters to a string in Kotlin, using List.joinToString() function, with the help of examples.
Kotlin – Convert list of characters to a string
To convert a list of characters to a string in Kotlin, you can use List.joinToString() function with an empty string as separator between the elements.
Call the joinToString() function on the list of characters, the function returns, a string created from joining the characters in the given list.
The syntax to convert the list of characters charList
to a string is
charList.joinToString(separator = "")
Example
In the following program, we make a list of characters in charList
and join them to a string.
Main.kt
fun main() {
val charList = listOf('h', 'e', 'l', 'l', 'o')
val str = charList.joinToString(separator = "")
println(str)
}
Output
hello
Conclusion
In this Kotlin Tutorial, we learned how to convert a list of characters to a string using List.joinToString() function.