java 分隔函数split("",-1)的用途
转:
java 分隔函数split("",-1)的用途
1.如果字符串最后一位有值,则没有区别,
2.若干最后n位都是切割符,split(" ")不会继续切分,split(" ", -1)会继续切分
-
String line = "a b c ";
-
String [] tmp = line.split(" ");
-
System.out.println(tmp.length+"------");
-
for(int i=0;i<tmp.length;i++){
-
System.out.println(i+"="+tmp[i]);
-
}
-
-
String [] items = line.split(" ",-1);
-
System.out.println(items.length+"========");
-
for(int i=0;i<items.length;i++){
-
System.out.println(i+"="+items[i]);
-
}
结果:
4------
0=a
1=b
2=
3=c
13========
0=a
1=b
2=
3=c
4=
5=
6=
7=
8=
9=
10=
11=
12=
-
[java] view plain copy
-
String line = "a b c d";
-
String [] tmp = line.split(" ");
-
System.out.println(tmp.length+"------");
-
for(int i=0;i<tmp.length;i++){
-
System.out.println(i+"="+tmp[i]);
-
}
-
-
String [] items = line.split(" ",-1);
-
System.out.println(items.length+"========");
-
for(int i=0;i<items.length;i++){
-
System.out.println(i+"="+items[i]);
-
}
结果:
13------
0=a
1=b
2=
3=c
4=
5=
6=
7=
8=
9=
10=
11=
12=d
13========
0=a
1=b
2=
3=c
4=
5=
6=
7=
8=
9=
10=
11=
12=d