The split method of string supports regular expressions;
The regular expression \s to match any white-space character, + to match one or more times. first, the string to be divided
String str = "a b c d E F g"
Where the white space in the string is: a single space, multiple Spaces, tab tabs. Two, use one or more spaces to split a string
The correct code is as follows:
String [] arr = Str.split ("\\s+");
for (String Ss:arr) {
System.out.println (ss);
}
As a contrast, the error code is as follows:
String [] arr2 = Str.split ("");
for (String ss:arr2) {
System.out.println (ss);
}
The test code is as follows:
public class stringmul{
//1. String split public
static STRING[]STR1 (string str)
{
System.out.println ("Method one: ");
String [] arr = Str.split ("");
return arr;
}
2. String split public
static STRING[]STR2 (string str)
{
System.out.println ("Method two:");
String [] arr = Str.split ("\\s+");
return arr;
}
public static void Main (String[]args)
{
String str = "AB cdef g";
STRING[]ARR=STR1 (str);
for (String Ss:arr) {
System.out.println (ss);
}
STRING[]ARR2=STR2 (str);
for (String ss:arr2) {
System.out.println (ss);
}
}
}