In this tutorial, you shall learn how to convert a given string into a list of characters in Kotlin, using String.toList() function, with examples.
Kotlin – Convert a String to List of Characters
To convert a given string into list of characters in Kotlin, call toList() method on this string. String.toList() function returns a List<Char> object with all the characters of this string as elements.
Example
In the following example, we take a string in str
, and convert it into a list of characters using toList() method.
Main.kt
fun main(args: Array<String>) { var str = "hello world" var result = str.toList() println(result) }
Output
[h, e, l, l, o, , w, o, r, l, d]
ADVERTISEMENT
Conclusion
In this Kotlin Tutorial, we learned how to convert a given string into list of characters using String.toList() method, with the help of Kotlin example programs.