String.split() 一个或多个空格分割字符串

待分隔字符串

//分别间隔一个空格,两个空格,一个tab制表符
String str = "a b  c    d";

效果

//四种分隔符
String[] arr1 = str.split(" ");
String[] arr2 = str.split("s");
String[] arr3 = str.split("\t");
String[] arr4 = str.split("\\s+");

//打印
for (String s : arr1) {
    System.out.print(s+",");
}
System.out.println("");
for (String s : arr2) {
    System.out.print(s+",");
}
System.out.println("");
for (String s : arr3) {
    System.out.print(s+",");
}
System.out.println("");
for (String s : arr4) {
    System.out.print(s+",");
}

参考博客

[1]https://blog.csdn.net/xueqinmax/article/details/81748034

你可能感兴趣的:(Java,EE)