public class TestSpilt { public static void main(String[] args){ String str=""; System.out.println(str.split("\\|").length);//输出1 } }
public String[] split(CharSequence input, int limit) { int index = 0; boolean matchLimited = limit > 0; ArrayList<String> matchList = new ArrayList<String>(); Matcher m = matcher(input); // Add segments before each match found while(m.find()) { if (!matchLimited || matchList.size() < limit - 1) { 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()};这是 split方法的源码。。一看就明白。。因为 正则 匹配不到 字符。。就直接返回一个数组 、数组中包着 空字符串 所以他的长度是1
String[] strs=" |asdf".split("\\|");
System.out.println(strs.length);
System.out.println(strs[0].equals(""));
System.out.println(strs[1]);
//事实胜于雄辩。
2
true
asdf
String[] strs="|asdf".split("\\|");//没有上面那个空格
System.out.println(strs.length);
System.out.println(strs[0].equals(""));
System.out.println(strs[1]);
2
true
asdf
String[] strs="".split("\\|");//没有上面那个空格
System.out.println(strs.length);
System.out.println(strs[0].equals(""));
}
public class TestSpilt {
public static void main(String[] args){
String str="";
System.out.println(str.split("\\|").length);//输出1
}
}
public String[] split(CharSequence input, int limit) {
int index = 0;
boolean matchLimited = limit > 0;
ArrayList<String> matchList = new ArrayList<String>();
Matcher m = matcher(input); // Add segments before each match found
while(m.find()) {
if (!matchLimited || matchList.size() < limit - 1) {
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()};这是 split方法的源码。。一看就明白。。因为 正则 匹配不到 字符。。就直接返回一个数组 、数组中包着 空字符串 所以他的长度是1