String toCharArray() Method

In Java, String toCharArray() Method is used to convert a String to new character array. The length of new character array created would be equal to the length of the String.

Signature of toCharArray Method:
char[] toCharArray()

Let’s take an example to understand toCharArray() method of String in Java.

Example : String toCharArray() Method
package com.masterjavatutorial;
public class TestExample {
	public static void main(String[] args) {
		String str ="HelloWorld"; 
		System.out.println("Length of String="+str.length());
		char[] charArray = str.toCharArray(); 
		System.out.println("Length of Char Array="+charArray.length);
        for(char ch:charArray){
        	System.out.println(ch);
        }
	}
}

The output of the above method is as below:

Length of String=10
Length of Char Array=10
H
e
l
l
o
W
o
r
l
d

We can see here that the length of char array created is equal to the length of String.