Here's a sample string which I intend to split into an array:
Hello My Name Is The Mighty Llama
The output should be:
Hello My
Name Is
The Mighty
Llama
The below splits on every space, how can I split on every other space?
String[] stringArray = string.split("\\s");
                You could do:
String[] stringArray = string.split("(?<!\\G\\S+)\\s");
                        While this is possible to use split to solve it like this one I strongly suggest using more readable way with Pattern and Matcher classes. Here is one of examples to solve it:
String string="Hello My Name Is The Mighty Llama";
Pattern p = Pattern.compile("\\S+(\\s\\S+)?");
Matcher m = p.matcher(string);
while (m.find())
    System.out.println(m.group());
output:
Hello My
Name Is
The Mighty
Llama
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With