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.
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
You can download the full source code of this example here: ToCharArray Java Example