Java字符串split方法的坑
先来看几行简单的Java代码,如下:
System.out.println("1,2".split(",").length);
System.out.println("1,2,".split(",").length);
System.out.println("".split(",").length);
System.out.println(",".split(",").length);
接下来,猜一下各行的输出结果。OK,下面给出真正的运行结果:
2
2
1
0
这里先给出jdk相关源码,再来对应分析各自的输出:
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
(1)one-char String and this character is not one of the
RegEx's meta characters ".$|()[{^?*+\\", or
(2)two-char String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
*/
char ch = 0;
if (((regex.value.length == 1 &&
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
(regex.length() == 2 &&
regex.charAt(0) == '\\' &&
(((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
((ch-'a')|('z'-ch)) < 0 &&
((ch-'A')|('Z'-ch)) < 0)) &&
(ch < Character.MIN_HIGH_SURROGATE ||
ch > Character.MAX_LOW_SURROGATE))
{
int off = 0;
int next = 0;
boolean limited = limit > 0;
ArrayList<String> list = new ArrayList<>();
while ((next = indexOf(ch, off)) != -1) {
if (!limited || list.size() < limit - 1) {
list.add(substring(off, next));
off = next + 1;
} else { // last one
//assert (list.size() == limit - 1);
list.add(substring(off, value.length));
off = value.length;
break;
}
}
// If no match was found, return this
if (off == 0)
return new String[]{this};
// Add remaining segment
if (!limited || list.size() < limit)
list.add(substring(off, value.length));
// Construct result
int resultSize = list.size();
if (limit == 0) {
while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
resultSize--;
}
}
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);
}
return Pattern.compile(regex).split(this, limit);
}
1.第一行代码的输出结果肯定没什么问题,字符串 "1,2" 以 "," 分隔,结果很直观的是 ["1", "2"],length=2。
2.第二行代码的输出结果,可能大家有人认为是length=3才对,因为字符串 "1,2," 以 "," 分隔,结果应该是 ["1", "2", ""],length=3;其实不然,jdk在split处理的时候,确实会先生成一个集合list = ["1", "2", ""],但之后却会循环判断末位元素是否为空字符串(即末位元素length=0),因此集合最终会变成 ["1", "2"],length=2。具体判断如下:
while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
resultSize--;
}
3.第三行代码的输出结果,数组 [""],length=1。与其他三种情况不同,空字符串 "" 中不包含regex字符串 ",",所以代表没有匹配上的子串(off=0),则返回字符串本身。具体处理如下:
// If no match was found, return this
if (off == 0)
return new String[]{this};
4.第四行代码的输出结果,可能也有部分人认为结果应是length=2,因为字符串 "," 以 "," 分隔,结果应该是 ["", ""],length=2;其实亦不然,与第2行同样的原理,最终将list=["", ""] 处理为空集合 [],length=0。
以上,系本文分享的split的一个小坑;除此之外,另一个需要注意的地方,split方法的参数是正则表达式而非一般字符串,所以在处理正则转义字符和特殊字符时留意即可。