In this tutorial, you shall learn how to convert a given character array to string in Kotlin, using String() constructor, with examples.
Kotlin – Convert char array to string
To convert a character array to string in Kotlin, use String() constructor. String() constructor can take a Char Array as argument and return a new string formed with the characters in the given array.
Syntax
The syntax to call String() constructor with Char Array chars
passed as argument is
</>
Copy
String(chars)
Examples
1. Convert array of character [‘a’, ‘p’, ‘p’, ‘l’, ‘e’] to a string
In the following example, we take an array of characters, and convert this character array to string using String().
Main.kt
</>
Copy
fun main(args: Array<String>) {
val chars = charArrayOf('a', 'p', 'p', 'l', 'e')
val str = String(chars)
println(str)
}
Output
apple
Conclusion
In this Kotlin Tutorial, we learned how to convert a Char Array to a String using String() constructor, with examples.