How do you use the split method to split an array of strings in Java?
In Java, you can use the split method to divide a string array. Here is an example:
public class Main {
public static void main(String[] args) {
String str = "apple,orange,banana,grape";
String[] fruits = str.split(",");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
In the example above, we start by defining a string containing multiple fruit names, and then we use a comma as a separator to split it into a string array. Next, we use a for loop to iterate through each element in the array and print it out. The output will be:
apple
orange
banana
grape