In this tutorial, you shall learn how to convert a given string to character array in Kotlin, using String.toCharArray() method, with examples.
Kotlin – Convert string to char array
To convert a string to character array in Kotlin, use String.toCharArray() method. String.toCharArray() method returns a Char Array created using the characters of the calling string.
Syntax
The syntax to call toCharArray() method on String str
is
</>
Copy
str.toCharArray()
Examples
1. Convert the string “apple” to character array
In the following example, we take a string and convert this to an array of characters using String.toCharArray() method.
Main.kt
</>
Copy
fun main() {
val str = "apple"
val chars = str.toCharArray()
for (x in chars) println("$x")
}
Output
a
p
p
l
e
Conclusion
In this Kotlin Tutorial, we learned how to convert a String to Char Array using String.toCharArray() method, with examples.