java.lang.String.split() : 你不知道的java小秘密

String.split():

-------------------------------------------  String test = "123,123,,123,,"; ------------------------------------------- //结尾的空格元素,不会作为arr的一个元素而返回 split(",");

 String[] arr1 = test.split(",");  System.out.println(arr1.length);  ==>4

------------------------------------------- //结尾的空格将不会作为arr的一个元素 split(",",0);//trailing empty strings will be discarded

 String[] arr2 = test.split(",", 0);  System.out.println(arr2.length);  ==>4

------------------------------------------- //当第二个参数<0时,表示不论是否是空格,都算一个结果 split(",",-1);

 String[] arr3 = test.split(",",-1);  System.out.println(arr3.length);  ==>6

------------------------------------------- //第二个参数>0,表示需要拆分出的数组个数 为这个参数 split(",",2);  String[] arr4 = test.split(",",2);  System.out.println(arr3.length);  ==>2  {"123" , "123,,123,,"}

原文地址:https://www.cnblogs.com/tomcatandjerry/p/2566063.html