The toCharArray() method converts a string to a char array in Java. This method lets you manipulate individual characters in a string as list items. Spaces, numbers, letters, and other characters are all added to the char array.
When coding, sometimes you’ll want to convert a Java string to char array. For instance, say you’re building an app that stores a list of student grades. You may want to convert those grades from a string to an array so you can manipulate them using array methods.
That’s where the toCharArray() Java method comes in. toCharArray() is a built-in Java method that is used to convert a string to an array of characters.
This tutorial will discuss using the toCharArray() method in Java, with reference to step-by-step examples of the method in action.
Java Strings and Chars
In Java, there are two data types used to store text-based data. Java strings store a sequence of one or more characters, and the char data type stores an individual character.
For instance, say you wanted to store the name of a student in a fifth-grade math class. That name is stored in a string because it contains multiple characters. But if you wanted to store the first letter of the student’s name, you would store the letter as a char.
This difference is important to note because the char data type is more efficient when you’re working with individual characters. The String class contains a lot of additional string-based methods that you do not need to access when you’re only working with one character.
Java String to Char Array
The Java toCharArray() method converts a Java string to a char array. Each character from the original string, including spaces, will appear as a separate value in the new char array.
Here is the syntax for the toCharArray() method:
char[] array_name = string.toCharArray();
The toCharArray() method has the following components:
- char[] tells our code we want to declare an array of chars.
- array_name is the name assigned to our new array.
- string is the string which we want to convert to a char array.
- toCharArray() converts the value of string to a character array.
The length of the resulting char array is equal to the length of the original string.
Let’s walk through an example and discuss how this method works step-by-step.
String to Char Array Java Example
Suppose we are creating a program that processes the grades given to students in a fifth-grade math class. These grades can be within the range of A and F, and are stored in a string like this:
ABCAAAC
However, we want to convert them to a char array so that we can use array methods on the grades. Here’s the code we would use to convert the string of student grades to a character array:
class ConvertGrades { public static void main(String args[]) { String grades = "ABCAAAC"; char[] grade_list = grades.toCharArray(); for (int i = 0; i < grade_list.length; i++) { System.out.print(grade_list[i]); } } }
The output of our code is as follows:
A B C A A A C
Let’s break down how our code works. First, we define a public class called ConvertGrades, which stores the code for our program. Then, we declare a Java variable called grades which stores the grades of the fifth-grade math class students.
On the next line, we use the string toCharArray() method to convert the contents of the grades string in Java to a char array. We assign the new array to the variable grade_list. Then, we initialize a for loop that loops through every item in the returned array. Each item is printed out to the console on a new line.
So, our program has converted our grades to a char array, then has printed them out to the console.
Converting a Java String to Char Array: Conclusion
The Java toCharArray() method is used to convert a sequence of characters stored in a string to an array of chars. In this tutorial, we covered how to use the toCharArray() method in Java.
You now have the knowledge you need to convert strings to char arrays in Java using toCharArray().
To learn more about writing Java code, read our How to Learn Java guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.