In this short tutorial, we’re going to shed light on how to convert a string to char array in Java.

First, we’ll see how to use Java 7 methods to achieve the intended purpose.

Then, we’re going to showcase how to use Java 8 to accomplish the Java String to Character array conversion.

For more details about string-to-array conversion, feel free to check our article here.

Convert String to Character array in Java 7

Java 7 provides several methods that we can use to turn a String into a Character array.

So, let’s go over them one by one.

Using an array with charAt() Method

Let’s start with the naive approach. The most simple and straightforward solution is to use a regular loop to read our String object character by character.

With the help of the charAt() method, we can get every character of the String and add it to a char array.

Let’s see how we can implement this solution in Java:

    
        public class Demo {
            public static Character[] toCharArray(String str) {
                if (str == null)
                    return null;

                Character[] chars = new Character[str.length()];
                for (int i = 0; i < str.length(); i++)
                    chars[i] = str.charAt(i);

                return chars;
            }

            public static void main(String[] args) {
                String str = "DevWithusCom";
                Character[] chars = Demo.toCharArray(str);
                for (char c : chars) {
                    System.out.print(c+" ");
                }
            }
        }
    

Now, let’s run the program and see the output:

    
        D e v W i t h u s C o m 
    

Using toCharArray() Method

String class provides toCharArray() as a built-in method to convert a given String to char array in Java.

toCharArray() returns a char[] array that contains the character sequence represented by the string.

It’s worth to mention that the length of the character array is the same as the String length.

The following example illusrates how to use toCharArray() to convert a particular String object to an array of characters:

    
        public class Demo {
            public static void main(String[] args) {
                String str = "StringToCharArray";
                char[] chars = str.toCharArray();
                for (char c : chars) {
                    System.out.print(c+" ");
                }
            }
        }
    

When we execute the above program, we will get the result as shown below:

    
        S t r i n g T o C h a r A r r a y
    

To learn how to convert a character array to a string, please refer to this article.

Using getChars() Method

getChars() is another great method that we can use to copy a String characters into a char array in Java.

In general, this method accepts four arguments and doesn’t return any value.

  • srcBegin: represents the index of the first character to copy.

  • srcEnd: the index after the last character of the string object.

  • dest: represents the destination char array.

  • destBegin: the first offset of the destination array.

    
        public class Demo {
            public static void main(String[] args) {
                String str = "String to char array Java";
                char[] chars = new char[str.length()];
                str.getChars(0, str.length(), chars, 0);
                for (char c : chars) {
                    System.out.print(c+" ");
                }
            }
        }
    

The output of the above program looks like this:

    
        S t r i n g   t o   c h a r   a r r a y   J a v a 
    

Please bear in mind that getChars() may throw IndexOutOfBoundsException if the srcBegin parameter is less than zero or srcEnd is greater than the length of the string.

String to Char array Java 8

In this section, we’re going to discuss how to convert a String object into an array of characters In Java 8.

So, without further ado, let’s go down the rabbit hole to see how to accomplish the String array conversion.

Using chars() Method

In Java 8, the String class comes with a new method called chars(). This method returns a Stream of int from a String object.

Simply put, the returned stream holds the int representation of each character.

    
        import java.util.Arrays;
        public class Demo {
            public static void main(String[] args) {
                String str = "WelcomeToDevWithUs";
                Character[] chars = str.chars()
                                       .mapToObj(c -> (char) c)
                                       .toArray(Character[]::new);
                System.out.println(Arrays.toString(chars));
            }
        }
    

As we can see, we use mapToObj(c -> (char) c) to convert each int to a character. That way, we can display the conversion result in a more human-friendly way.

    
        W e l c o m e T o D e v W i t h U s 
    

Convert with codePoints() Method

Alternatively, Java 8 provides the codePoints() method to return an IntStream from a particular String.

In short, code points refers to Unicode codepoints. So, the idea behind codePoints() is to handle effectively the Unicode supplementary characters.

    
        import java.util.Arrays;
        public class Demo2 {
            public static void main(String[] args) {
                String str = "HelloDearReaders";
                Character[] chararr = str.codePoints()
                                         .mapToObj(c -> (char) c)
                                         .toArray(Character[]::new);
                System.out.println(Arrays.toString(chararr));
            }
        }
    

String to Character array with ArrayUtils

Apache Commons Lang library provides a set of helper utilities to simplify String and array manipulation.

ArrayUtils is one these helper classes. It provides the toObject(char[] array) method to convert an array of chars to a Character[] array.

    
        import org.apache.commons.lang.ArrayUtils;
        import java.util.Arrays;
        public class Demo{
            public static void main(String args[]){
                String str = "DevWithUsBlog";
                Character[] chars = ArrayUtils.toObject(str.toCharArray());
                for (char c : chars) {
                    System.out.print(c+" ");
                }
            }
        }
    

Conclusion

In this tutorial, we have explored different ways to convert a String to char array in Java.

Along the way, we used methods that are part of Java 7 and Java 8, then we saw how to make use of the Apache Commons ArrayUtils class.