Java toCharArray Method

The Java toCharArray String Method is helpful for converting the user-specified string into Character Array (Char Array). In this article, we show how to use this Java String toCharArray to convert the string of words into a char array with an example.

The syntax of the String toCharArray in Java Programming language is shown below. The following method will not accept any parameters. However, it converts the string object into Character Array.

public char[] toCharArray(); 

//In order to use in program
String_Object.toCharArray()

Java toCharArray Method Example

This program uses the Java String tochararray method to convert the user-specified string object into Character Array. First, we declared string variables with sample text.

The following String Method statement in this program converts every character present in the previously declared str into a char Array.

Next, we used the Foreach Loop to iterate every character present in the newly created Character Array Ch. And it assigns each character to char cr.

Within the Java Foreach loop, we used the System.out.println statement. It will print the characters as an output.

package StringFunctions;

public class ToCharArrayMethod {
	public static void main(String[] args) {
		String str = "Tutorial GateWay";
		
		char[] ch = str.toCharArray();
		for (char cr: ch) {
			System.out.println("Character Array Element = " + cr); 
		}
	}
}
Java toCharArray 1