Home » Core Java » ToCharArray Java Example

About Mehdi Shahdoost

Avatar photo
In a nutshell, I am an enthusiastic Java developer with more than 10 years of experience ranging from banking solutions to national and startup projects. I have worked with Java since 1.3 version until now. Moreover, I am passionate about the Java ecosystem. I can't imagine myself doing something other than programming.

ToCharArray Java Example

In this article, we will learn how to convert a string to a char array using the toCharArray Java method. We will explore many ways of converting a string to an array of characters in different versions of Java.

ToCharArray Java

1. charAt() method ( Verbose way )

In this way, we create a char array with the exact size of a string. Then, we iterate over a string and copy each character into a char array.

import java.util.Arrays;

public class CharAtExample {

    public static void main(String[] args) {

        String javaCodeGeeks = "JavaCodeGeeks";
        // Char array with exact size of the string
        char[] javaCodeGeeksArray = new char[javaCodeGeeks.length()];
        
        // Loop over string and get each string's character with charAt() method
        for (int i = 0; i < javaCodeGeeks.length(); i++) {
            javaCodeGeeksArray[i] = javaCodeGeeks.charAt(i);            
        }
        
        // Prints elements of char array by Arrays.toString method
        System.out.println(Arrays.toString(javaCodeGeeksArray));
    }
}

As we can see from the above example, this solution is very verbose and need more steps to get an array of characters from a string. In the next section we will explore an efficient way to do this job.

2. toCharArray() method in Java

In this approach, we will use a magical method of String class. toCharArray() method returns an array of characters without any effort.

import java.util.Arrays;

public class ToCharArrayExample {

    public static void main(String[] args) {

        String javaCodeGeeks = "JavaCodeGeeks";
        char[] javaCodeGeeksArray = javaCodeGeeks.toCharArray();

        System.out.println(Arrays.toString(javaCodeGeeksArray));
    }
}

As we can see from the above example, with calling a direct method on our string we get a char array. This is the best way with minimum lines of code to get an array of characters from a string.

3. IntStream ( Java8 )

In java 8, String class implements CharSequence interface in which chars() method returns Stream of int value. We can use IntStream to get an array of characters.

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class IntStreamToCharArrayExample {

    public static void main(String[] args) {

        String javaCodeGeeks = "JavaCodeGeeks";
        // Gets IntStream of string
        IntStream chars = javaCodeGeeks.chars();
        // Map int value to char
        Stream<Character> characterStream = chars.mapToObj(c -> (char) c);
        // Converts stream of characters into Character array.
        Character[] characters = characterStream.toArray(Character[]::new);

        System.out.println(Arrays.toString(characters));
    }
}

In java 8, we use chars() method of String class to get IntStream from the string. Subsequently, we map each int value into char. Finally, we convert stream of characters into a char array.

4. Summary

The Simplest way to convert a string into char array is toCharArray() method of String class. We can also iterate over a string and retrieve each character with the charAt() method of string class. In java 8, we can use the chars() method of String class and convert IntStream into Character array.

5. Download the source code

Download
You can download the full source code of this example here: ToCharArray Java Example

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments