I have a CharArray
whose contents are characters like:
val chars = arrayOf('A', 'B', 'C')
or
val chars = "ABC".toCharArray()
I want to get the string "ABC"
from this. How can I do it?
chars.toString()
does not work; it works as if chars
was a normal integer array.
We can convert a char to a string object in java by using the Character. toString() method.
So how to convert String array to String in java. We can use Arrays. toString method that invoke the toString() method on individual elements and use StringBuilder to create String. We can also create our own method to convert String array to String if we have some specific format requirements.
you can simply using Array#joinToString
:
val result: String = chars.joinToString("");
OR convert chars
to CharArray
:
val result: String = String(chars.toCharArray());
OR declaring a primitive CharArray
by using charArrayOf
:
val chars = charArrayOf('A', 'B', 'C'); val result: String = String(chars);
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