- public class Split {
-
- public static void main(String[] args) {
-
- String str1 ="1,2,3,4,5";
- String str2 ="1,2,3,4,";
- String str3 ="1,,2,3,";
- String str4=",,,,";
-
- System.out.println(str1.split(",").length);
- System.out.println(str2.split(",").length);
- System.out.println(str3.split(",").length);
- System.out.println(str4.split(",").length);
-
- }
-
- }
复制代码
得到的结果为:
上面的这段只对str4有效.如果想把4个字符串都获取为5个长度的话,应该使用str3.split(",", -1);
|
|