fun main(args: Array<String>) { val StringCharacter = "A" val CharCharacter = StringCharacter.toChar() println(CharCharacter) }
I am unable to convert string A to char. I know that StringCharacter = 'A' makes it char but I need the conversion.
Thanks.
Returns an element at the given index or null if the index is out of bounds of this array. fun CharArray.
A CharSequence
(e.g. String
) can be empty, have a single character, or have more than one character.
If you want a function that "returns the single character, or throws an exception if the char sequence is empty or has more than one character" then you want single
:
val string = "A" val char = string.single() println(char)
And if you want to call single
by a different name you can create your own extension function to do so:
fun CharSequence.toChar() = single()
Usage:
val string = "A" val char = string.toChar() println(char)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With