Knowledge Supplement
The split method of string supports regular expressions;
The regular expression \s
represents any white space character that matches +
one or more occurrences.
With the above supplementary knowledge, the following content is very well understood.
A. String to be split
The string to be split is as follows:
str = "a b c d e f g"
Where the whitespace in the string is: a single space, multiple Spaces, tab tabs.
Second, use one or more spaces to split the string
The correct code is as follows:
String [] arr = str.split("\\s+");for(String ss : arr){ System.out.println(ss);}
As a comparison, the error code is as follows:
String [] arr2 = str.split(" ");for(String ss : arr2){ System.out.println(ss);}
Third, the result of segmentation
To split the result with the correct code:
abcdefg
To split the result with an error code:
abcdefg
四:编写的程序
Public Static void Main (String args[]) { = "good12 morning34 good56 night78"; // The output of the array after the split of string String[] Tt=line.split ("\\s+"); for (String s:tt) { System.out.println (s); } System.out.println ("#####");}
The output result is
Good12
Morning34
Good56
night78
JAVA one or more spaces split string