字符串用split方法进行分割的几种特殊情况:
public class SplitDemo {
public static void main(String[] args) {
String str = "AB";
String[] split = str.split("/");
System.out.println(str + "用'/'分割后数组长度:" + split.length); // 1
System.out.println(str + "用'/'分割后数组:" + Arrays.toString(split)); // [AB]
System.out.println("---------------------------------------");
String[] split2 = str.split(",");
System.out.println(str + "用','分割后数组长度:" + split2.length); // 1
System.out.println(str + "用','分割后数组:" + Arrays.toString(split2)); // [AB]
System.out.println("---------------------------------------");
String s = "A/";
String[] split3 = s.split("/");
System.out.println(s + "用'/'分割后数组长度:" + split3.length); // 1
System.out.println(s + "用'/'分割后数组:" + Arrays.toString(split3)); // [A]
System.out.println("---------------------------------------");
String ss = "/";
String[] split4 = ss.split("/");
System.out.println(ss + "用'/'分割后数组长度:" + split4.length); // 0
System.out.println(ss + "用'/'分割后数组:" + Arrays.toString(split4)); // []
System.out.println("---------------------------------------");
String sss = "";
String[] split5 = ss.split("/");
System.out.println(sss + "用'/'分割后数组长度:" + split5.length); // 0
System.out.println(sss + "用'/'分割后数组:" + Arrays.toString(split5)); // []
}
}
上面几种情况运行结果: