首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java String种的split方法

2012-12-28 
java String类的split方法public String[] split(CharSequence input, int limit) {int index 0boolean

java String类的split方法

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()};        // 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);    }

对前面的==和后面的==处理的方式不一样,最终的输出结果为:
引用4


he
llo
这是不是个bug,他把后面==分割出来的空串忽略了,前面的确没有。

热点排行