Kotlin – Convert String into an Array of Characters

Convert string into an array of characters in Kotlin

To convert a given string into an array of characters in Kotlin, call toCharArray() function on the given string.

str.toCharArray()

The function returns an array of characters.

Input string value
"apple"

Output char array
['a', 'p', 'p', 'l', 'e']

Examples

Convert the string “apple” into an array of characters

In the following program, we take a string in str and convert this string into an array of characters.

Kotlin Program

fun main() {
    val str = "apple"
    val chars = str.toCharArray()
    println(chars.contentToString())
}

Output

[a, p, p, l, e]