Convert character array to string in Kotlin
To convert a given character array to a string type value in Kotlin, you can use joinToString()
function of the Array class.
Call joinToString()
function on the given character array object, say charArray
, and pass an empty string as separator between the elements of array.
charArray.joinToString("")
The function returns a string value created from joining the elements of the given character array with an empty string as separator.
Examples
Convert given character array to a string
In the following program, we take a character array initialised with five characters in charArray
variable. We convert this character array to a string value and print it to output.
Kotlin Program
fun main() {
val charArray = arrayOf('a', 'p', 'p', 'l', 'e')
val str = charArray.joinToString("")
println(str)
}
Output
apple