关于String中split方法的应用疑惑???
1,The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
在上面的例子中, 当以“o”来作为Regex时,Result中 { "b", "", ":and:f" }的第二个元素"",我想问下这个empty strings是怎么产生的呢?谢谢!!!
1,The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
在上面的例子中, 当以“o”来作为Regex时,Result中 { "b", "", ":and:f" }的第二个元素"",我想问下这个empty strings是怎么产生的呢?谢谢!!!
int index = 0;
boolean matchLimited = limit > 0;
ArrayList matchList = new ArrayList();
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()}; // 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 (String[])matchList.subList(0, resultSize).toArray(result);
}
像"abcdefg"用a|c分割后,就是"","b","defg"