Converting String to "Character" array in Java

kgd picture kgd · Apr 4, 2012 · Viewed 507.7k times · Source

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

Kuldeep Jain picture Kuldeep Jain · Apr 4, 2012

Use this:

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