Kotlin List toCharArray()
Syntax & Examples


Syntax of List.toCharArray()

The syntax of List.toCharArray() extension function is:

fun Collection<Char>.toCharArray(): CharArray

This toCharArray() extension function of List returns an array of Char containing all of the elements of this collection.



✐ Example

1 Example

In this example,

  • We create a list named list containing the characters 'a', 'b', 'c'.
  • We call the toCharArray() function on list, which converts the list to a Char array.
  • The resulting Char array is stored in result.
  • Finally, we print the content of result to standard output using the contentToString() function.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf('a', 'b', 'c');
    val result = list.toCharArray();
    println(result.contentToString());
}

Output

[a, b, c]

Summary

In this Kotlin tutorial, we learned about toCharArray() extension function of List: the syntax and few working examples with output and detailed explanation for each example.