kgd kgd - 5 months ago 17
Java Question

Converting String to "Character" array in Java

I want to convert a

String
to an array of objects of Character class but I am unable to perform the conversion. I know that I can convert a String to an array of primitive datatype type "char" with the
toCharArray()
method but it doesn't help in converting a String to an array of objects of Character type.

How would I go about doing so?

Answer

Use this:

String str = "testString";
char[] charArray = str.toCharArray();
Character[] charObjectArray = ArrayUtils.toObject(charArray);