String split = " egg book apple pear apricot ";
String[] arrs = split.split(" ");
System.out.println();
int i = 1;
for (String arr : arrs) {
System.out.println("----" + (i++) + "--→" + arr + "←--");
}
----1--→←--
----2--→egg←--
----3--→book←--
----4--→apple←--
----5--→←--
----6--→pear←--
----7--→←--
----8--→←--
----9--→apricot←--
String split = " egg book apple pear apricot ";
String[] arrs = split.split("\s+");
System.out.println();
int i = 1;
for (String arr : arrs) {
System.out.println("----" + (i++) + "---" + arr);
}
----1---
----2---egg
----3---book
----4---apple
----5---pear
----6---apricot
1、后空不留前空留
2、单个替换和整体替换的区别