0 votes
1 view
in Java by (900 points)

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?

1 Answer

0 votes
by (13.2k points)

There are two easy ways of doing this -

  1. You can first convert string to char using toCharArray() , then char to Character

String testString = "myString";

char[] testArray = testString.toCharArray();

Character[] charObjectArray = ArrayUtils.toObject(testArray);

  1. For Java 8, you can use this 

String testString = "myString";

Character[] charObjectArray =    testString.chars().mapToObj(c -> (char)c).toArray(Character[]::new);

...