str.split(",",-1);
package com.example;
public class SplitTest { public static void main(String[] args) {
/** * 1.如果字符串最后一位有值,则没有区别, 2.如果最后n位都是切割符,split(" ")不会继续切分,split(" ", -1)会继续切分 */ String str = "1,2,3,4,5, , , , ,10,,,,";
String[] strArray = str.split(",");
System.out.println("str1.split(\",\"):strArray 长度为:" + strArray.length);
printInfo(strArray);
String[] strArray2 = str.split(",", -1);
System.out.println(); System.out.println("str1.split(\",\", -1):strArray2 长度为:" + strArray2.length);
printInfo(strArray2); }
private static void printInfo(String[] strArray) { for (int i = 0; i < strArray.length; i++) { System.out.println("i=" + i + " " + strArray[i] + " "); } }
}
结果
str1.split(","):strArray 长度为:10
i=0 1
i=1 2
i=2 3
i=3 4
i=4 5
i=5
i=6
i=7
i=8
i=9 10
str1.split(",", -1):strArray2 长度为:24
i=0 1
i=1 2
i=2 3
i=3 4
i=4 5
i=5
i=6
i=7
i=8
i=9 10
i=10
i=11
i=12
i=13
标签:String,strArray,System,split,str,strArray2 From: https://www.cnblogs.com/lbzwd/p/17295012.htmlhttps://blog.csdn.net/bk_huangzongbo/article/details/100014072