[Solved] How to split string into an array? [closed]


Split the string into array:

String x = "What is my name";
String[] splitted = x.split(" ");
for (int i = 0; i < Array.getLength(splitted); i++) {
    System.out.println("Word " + (i+1) + ": " + splitted[i]);
}

You can change System.out.println() to your desired output method.

Edit (as suggested in comment): For safer code, change Array.getLength(splitted) to splitted.length.

String x = "What is my name";
String[] splitted = x.split(" ");
for (int i = 0; i < splitted.length; i++) {
    System.out.println("Word " + (i+1) + ": " + splitted[i]);
}

4

solved How to split string into an array? [closed]