How to Convert a String to char array in Java?

In the last tutorial, we have learned how to convert a string to uppercase in Java. We have learned all the two variants of toUpperCase in Java. In this tutorial, we will learn how to convert a string to a char array in Java. We can convert a string to char array by using the toCharArray() method of Java String class. It returns a newly allocated character array whose length is the length of this string. 

Syntax: The syntax of the Java String toCharArray() method is given below:


puyblic char[] toCharArray
It converts this string to a new character array.


Java program to convert a string to char array.


class toCharArray{
	public static void main(String args[]){
		String str = "Java";
		
		//converting string to char array.
		char ch[] = str.toCharArray();
		
		//printing the char array.
		for(int i=0; i<ch.length; i++){
			System.out.println(ch[i]);
		}
	}
}

Output:
How to Convert a String to char array in Java?
How to Convert a String to char array in Java? How to Convert a String to char array in Java? Reviewed by Prashant Srivastava on January 08, 2020 Rating: 5

No comments:

Powered by Blogger.