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:
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]);
}
}
}
How to Convert a String to char array in Java?
Reviewed by Prashant Srivastava
on
January 08, 2020
Rating:
![How to Convert a String to char array in Java?](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCQxoPY1fY-plojviCRfU0cDZUOpS3Po15pyTdq_nN7t-lsg7aC0Hxgrrak1ZmKFSIg2G4TO_S86gTUuZUIccWHy0uT8PZx3fSnmwqKBKV8w-E0eImw645Q8IuZs4-824QkdGoGr2WAVCM/s72-c/Screenshot+%2528507%2529.png)
No comments: