在java里,split()方法如果只传入分割字符,那么会自动去除结尾的空字符串。
以前一直不知道,到底写了多少隐藏的bug,ヽ(゜ロ゜;)ノ
情况1
String str = "6000,5000"; String[] s = str.split(","); System.out.println(Arrays.toString(s)); System.out.println(s.length);
|
情况2
String str = "6000,5000"; String[] s = str.split(",", -1); System.out.println(Arrays.toString(s)); System.out.println(s.length);
|
情况3
String str = "6000,"; String[] s = str.split(","); System.out.println(Arrays.toString(s)); System.out.println(s.length);
|
情况4
String str = "6000,"; String[] s = str.split(",", -1); System.out.println(Arrays.toString(s)); System.out.println(s.length);
|
情况5
String str = ",6000"; String[] s = str.split(","); System.out.println(Arrays.toString(s)); System.out.println(s.length);
|
情况6
String str = ",6000"; String[] s = str.split(",", -1); System.out.println(Arrays.toString(s)); System.out.println(s.length);
|
情况7
String str = ","; String[] s = str.split(","); System.out.println(Arrays.toString(s)); System.out.println(s.length);
|
情况8
String str = ","; String[] s = str.split(",", -1); System.out.println(Arrays.toString(s)); System.out.println(s.length);
|
情况9
String str = ""; String[] s = str.split(","); System.out.println(Arrays.toString(s)); System.out.println(s.length);
|
情况10
String str = ""; String[] s = str.split(",", -1); System.out.println(Arrays.toString(s)); System.out.println(s.length);
|
总结
split()不传limit参数时会自动去除结尾的空字符串。
上述所有代码都可以直接运行。