今天在使用split分割字符串时突然想到一种情况,如下:
String str="aaaaaaaab";
String arr[]=str.split("aa");
问,arr数组的长度是多少?
那如果str为”baaaaaaaa”呢
String str="baaaaaaaa";
如果str=”aaaaaaaab”呢
String str="aaaaaaaab";
如果str=”baaaaaaaab”呢
String str="baaaaaaaab";
好,我们先在程序中验证一下:
public class Test {
public static void main(String[] args) {
String str="aaaaaaaa";
String [] arr=str.split("aa");
System.out.println("字符串aaaaaaaa分割的数组长度为:"+arr.length);
str="baaaaaaaa";
arr=str.split("aa");
System.out.println("字符串baaaaaaaa分割的数组长度为:"+arr.length);
str="aaaaaaaab";
arr=str.split("aa");
System.out.println("字符串aaaaaaaab分割的数组长度为:"+arr.length);
str="baaaaaaaab";
arr=str.split("aa");
System.out.println("字符串baaaaaaaab分割的数组长度为:"+arr.length);
}
}
运行以上代码输出结果
看到结果的你是不是有点小小的惊讶,如果有的话那就继续往下看。
通过split方法查看源码可知又调用了split(regex, 0)方法并且传入一个0:
public String[] split(String regex) {
return split(regex, 0);
}
继续查看源码
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);
}
有其中关系可知最终会执行 Pattern.compile(regex).split(this, limit)这一段代码,基础往下扒代码:
public String[] split(CharSequence input, int limit) {
int index = 0;
boolean matchLimited = limit > 0;
ArrayList<String> matchList = new ArrayList<>();
Matcher m = matcher(input);
// Add segments before each match found
while(m.find()) {
if (!matchLimited || matchList.size() < limit - 1) {
if (index == 0 && index == m.start() && m.start() == m.end()) {
// no empty leading substring included for zero-width match
// at the beginning of the input char sequence.
continue;
}
String match = input.subSequence(index, m.start()).toString();
matchList.add(match);
index = m.end();
} else if (matchList.size() == limit - 1) { // last one
String match = input.subSequence(index,
input.length()).toString();
matchList.add(match);
index = m.end();
}
}
// If no match was found, return this
if (index == 0)
return new String[] {input.toString()};
// Add remaining segment
if (!matchLimited || matchList.size() < limit)
matchList.add(input.subSequence(index, input.length()).toString());
// Construct result
int resultSize = matchList.size();
if (limit == 0)
while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
resultSize--;
String[] result = new String[resultSize];
return matchList.subList(0, resultSize).toArray(result);
}
通过代码我们可以发现最终matchList集合中会有值,不过都是空值,然后在
while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
resultSize--;
这一段代码中,首先判断最后一个是不是空,如果没有值的话就减一位,依次类推,所以看到这大家对以上程序出现的结果是不是就不奇怪了。
所以我们可以大胆的总结一下,使用split方法分割字符串,如果最后几位是空的话,会将空的位置去掉。
加入我的行列:
(嘿嘿嘿,点击这里关于我哦,分享小知识):https://www.jianshu.com/p/7d19f0df5b6b