I have the following:
String string = "1-50 of 500+";
String[] stringArray = string.split(" ");
Printing out all the elements in this array gives me the following:
Element 1: 1-50
Element 2: of 500+
How can I get it to split elements by the requirement that there is at least one whitespace between the words?
In other words, I want my elements to be:
Element 1: 1-50
Element 2: of
Element 3: 500+
解决方案
Use \\s+ to split on spaces even if they are more.
String string = "1-50 of 500+";
String[] stringArray = string.split("\\s+");
for (String str : stringArray)
{
System.out.println(str);
}
EDIT:
If you also want to split on tabs, change the regex to \\s+|\\t+ and it detects both spaces and tabs as well.