public String[] split(CharSequence input, int limit) {//input="123456" limit=0 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()) {//为什么返回true 而且返回7次??? if (!matchLimited || matchList.size() < limit - 1) { String match = input.subSequence(index, m.start()).toString(); matchList.add(match); index = m.end();//每次返回的m.start(),m.end()都相同,0 0, 1 1,2 2...,这样的话调用subSequence取不到数值的啊,那为什么matchList中最后还有值呢? } 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); } to All: 综上总结: "123456".split("") 在find()时是返回true的,而且返回7次, 为什么? 运行结果matchList的长度是8,而且最后一个数据是"",返回时没有被选取 最后的返回数组是"" "1","2","3","4","5","6"(如果加上那个未被返回的""就是8个数据了,请问为什么会产生8个数据呢?)写的一个测试程序: public static void main(String...args){ String str = "123456"; /*String[] strs = str.split("");
你是想使用多個空格來拆分嗎?
String[] str_array = str.split("\\s+");
字符串中有六个分割处str.substring(0, 0)
str.substring(1, 1)
str.substring(2, 2)
str.substring(3, 3)
str.substring(4, 4)
str.substring(5, 5)而str.substring(6, 6)则不存在,因为字串起始索引6>=str.length();
所以可以分割为7处:
str.substring(0, 0)
str.substring(0, 1)
str.substring(1, 2)
str.substring(2, 3)
str.substring(3, 4)
str.substring(4, 5)
str.substring(5, 6)
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()) {//为什么返回true 而且返回7次???
if (!matchLimited || matchList.size() < limit - 1) {
String match = input.subSequence(index, m.start()).toString();
matchList.add(match);
index = m.end();//每次返回的m.start(),m.end()都相同,0 0, 1 1,2 2...,这样的话调用subSequence取不到数值的啊,那为什么matchList中最后还有值呢?
} 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);
}
to All:
综上总结:
"123456".split("") 在find()时是返回true的,而且返回7次, 为什么?
运行结果matchList的长度是8,而且最后一个数据是"",返回时没有被选取
最后的返回数组是"" "1","2","3","4","5","6"(如果加上那个未被返回的""就是8个数据了,请问为什么会产生8个数据呢?)写的一个测试程序:
public static void main(String...args){
String str = "123456";
/*String[] strs = str.split("");
System.out.println(strs.length);
for(int i=0;i<strs.length;i++){
System.out.println(strs[i]);
}*/
Pattern pattern = Pattern.compile("");
Matcher matcher = pattern.matcher(str);
// System.out.println(matcher.matches());
// System.out.println(matcher.groupCount());
int i = 0;
while(matcher.find()){
// System.out.println("截取字符串:"+str.substring(matcher.start(),matcher.end()+1));
System.out.println("匹配次數:"+i++);
System.out.println("匹配字符串:"+matcher.group());
System.out.println("start:"+matcher.start());
System.out.println("end:"+matcher.end());
}
}
结果:
匹配次數:0
匹配字符串:
start:0
end:0
匹配次數:1
匹配字符串:
start:1
end:1
匹配次數:2
匹配字符串:
start:2
end:2
匹配次數:3
匹配字符串:
start:3
end:3
匹配次數:4
匹配字符串:
start:4
end:4
匹配次數:5
匹配字符串:
start:5
end:5
匹配次數:6
匹配字符串:
start:6
end:6从结果中可以看出匹配了7次,每次返回的匹配字符串都是""(不知道为什么?) 而且start()和end()每次都一样,这在subSequence(start,end) 中是取不到子字符串的啊!希望大家帮帮我!多谢!
0 0 -> ""
0 1 -> "1"
1 2 -> "2"
2 3 -> "3"
3 4 -> "4"
4 5 -> "5"
5 6 -> "6"
6 6 -> ""(为什么下标能取到6呢? 不越界吗?)
"" | 1 | 2 | 3 | 4 | 5 | 6 | "" 比較難理解的是前後,但既然是用 "" 做分割,前後當然也算空字串。既然 123456 用""分割應該有8個值,那為什麼"123456".split("")只得到7個?
是因為split有另一個method split(String regularString, int limit)
limit > 0 限制最多輸出值的個數,輸出值小於limit沒影響,大於limit就只輸出 limit 個值。
limit = 0 如果最後一個是空值,就捨棄最後一個。(limit 預設值是 0)
limit < 0 果最後一個是空值,一樣輸出。"123456".split("") 等於 "123456".split("",0) 所以只有7個值。
如果改成 "123456".split("",-1) 就會得到8個值。