Java String Handling

Java convert String to character array Example

This Java example shows how to convert String to char array in Java. This Java example also shows how to convert String to Character array and convert char array to String in Java.

How to convert String to char array in Java?

Use toCharArray method of String class to convert Java String object to char array.

This method creates a new character array whose length is equal to the length of the String object containing character sequence of the String object.

Output

How to convert String to Character object array in Java?

There is no direct method to convert String to Character object array in Java. Below given are two solutions.

a) Using for loop

Basically we have created a Character array whose length is equal to the length of the String and populated it one character at a time.

b) Using Apache Commons lang library

If you have Apache Commons lang library, you can use toObject method of ArrayUtils class to convert primitive char array to Character object array as given below.

This method converts an array of primitive char to objects.

How to convert char array to String?

Use String constructor that accepts char array as an argument and returns String representation of character sequence.

This constructor creates and returns a new String object containing sequence of characters contained in the char array.

Output

Please let us know your views in the comments section below.

 

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.