java的split方法我觉得大家都很常用,不知道大家有没有发现过下面这个有趣的现象,先别看答案,猜猜是否和你的预期结果一样
、、、
public class SplitTest {
public static void main(String[] args) {
String a= "1,2,3";
String b= "1,2,3,";
String c= "1,2,3,,";
String d= ",,1,2,3";
String e= "1,2,,3";
System.out.println(a.split(",").length);
System.out.println(b.split(",").length);
System.out.println(c.split(",").length);
System.out.println(d.split(",").length);
System.out.println(e.split(",").length);
}
}
、、、
测试结果:
3
3
3
5
4