String s1 = ",,,"; System.out.println(s1.split(",").length); // 0 String s2 = "a,,,"; System.out.println(s2.split(",").length); // 1 String s3 = ",a,,"; System.out.println(s3.split(",").length); // 2 String s4 = ",,a,"; String[] split = s4.split(","); System.out.println(split.length); // 3 Arrays.stream(split).forEach(System.out::println); // 空空a String s5 = "abcde"; System.out.println(s5.split("").length); // 5
※只會分割非分割符的前面,如果将 a 改成空格也是一樣的結果
※分割符什麼都沒有時,會每個字符都分割
※s1 長度是 0,所以使用 s1.split(",")[0] 會報 ArrayIndexOutOfBoundsException
沒有留言:
張貼留言