Java的String物件中,有個split function,
平常使用String.split("分隔字串")均可以將字串做分隔,
但,當原字串中,尾端若有連續出現的"分隔字串"時,
卻會被忽略。
例:
"a,b,c,d".split(",") = [a, b, c, d]
"a,b,c,d,,,".split(",") = [a, b, c, d]
但若改為
"a,b,c,d,,,".split(",", -1) = [a, b, c, d, "", "", ""]
原因為
"a,b,c,d,,,".split(",") = "a,b,c,d,,,".split(",", 0)
於是,尾端的空白值都會被忽略。
Java API Doc裡有提到:
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.