The toCharArray() extension function in Kotlin returns an array of Char containing all of the elements of the collection.
The syntax of Set.toCharArray() extension function is:
fun Collection<Char>.toCharArray(): CharArray
This toCharArray() extension function of Set returns an array of Char containing all of the elements of this collection.
Set.toCharArray() returns value of type CharArray
.
Using toCharArray() to convert a set of Char values to a CharArray.
For example,
fun main() {
val charSet = setOf('a', 'b', 'c')
val charArray = charSet.toCharArray()
println(charArray.joinToString())
}
a, b, c
Using toCharArray() to handle an empty set of Char values.
For example,
fun main() {
val emptySet = emptySet<Char>()
val charArray = emptySet.toCharArray()
println(charArray.joinToString())
}
Using toCharArray() to convert a set of mixed Char values to a CharArray.
For example,
fun main() {
val mixedSet = setOf('x', 'y', 'z', 'a')
val charArray = mixedSet.toCharArray()
println(charArray.joinToString())
}
x, y, z, a
In this Kotlin tutorial, we learned about toCharArray() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.