Kotlin List toCharArray()
Syntax & Examples
Syntax of List.toCharArray()
The syntax of List.toCharArray() extension function is:
fun Collection<Char>.toCharArray(): CharArrayThis 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
listcontaining the characters'a', 'b', 'c'. - We call the
toCharArray()function onlist, which converts the list to a Char array. - The resulting Char array is stored in
result. - Finally, we print the content of
resultto standard output using thecontentToString()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.