How to convert a string to an array in Java?

To convert a string into an array, you can use the toCharArray() method of the string. This method will return a character array, with each element being a character from the string.

Here is an example code:

String str = "Hello World";
char[] charArray = str.toCharArray();

for (char c : charArray) {
    System.out.println(c);
}

This will output each character in the string.

H
e
l
l
o

W
o
r
l
d
bannerAds