String[] split = str.split(",");
先说结论吧,
一、str.split(",")
方法会将后面为空的情况全部舍弃,前面为空的不会舍弃;
二、如果不想舍弃后面为空的情况,需要使用方法str.split(",", -1);
,没有-1的参数时,默认为0;
以下是实验:
1、数据规则时
String str = "0,1,2,3,4,5,6,7,8,9";
String[] split = str.split(",");
for (String s : split) {
System.out.print(s + "-");
}
---------------------------
结果为:0-1-2-3-4-5-6-7-8-9-
2、前面为空的情况
String str = ",,,3,4,5,6,7,8,9";
String[] split = str.split(",");
for (String s : split) {
System.out.print(s + "-");
}
---------------------------
结果为:---3-4-5-6-7-8-9-
3、后面为空的情况
String str = "0,1,2,3,4,5,6,,,";
String[] split = str.split(",");
for (String s : split) {
System.out.print(s + "-");
}
---------------------------
结果为:0-1-2-3-4-5-6-
4、使用str.split(",", -1);
方法,且后面为空的情况
String str = "0,1,2,3,4,5,6,,,";
String[] split = str.split(",", -1);
for (String s : split) {
System.out.print(s + "-");
}
---------------------------
结果为:0-1-2-3-4-5-6----