Java中的StringTokenizer类 分割字符串

这个类用的比较少,API文档上已经不推荐使用。推荐使用 Stringsplit 方法或 java.util.regex 包。

import java.util.StringTokenizer;

public class StringTokenizerTest {
    public static void main(String[] args) {
        String str = "stay hungry , stay foolish .";
        StringTokenizer st = new StringTokenizer(str," ");
        while (st.hasMoreTokens()) {
             System.out.print(st.nextToken()+" ");
         }
    }
}

分割str 输出stay hungry , stay foolish .

 

而使用String的split方法也能做到

String[] temp = str.split(" ");
        for (int i = 0; i < temp.length; i++) {
            System.out.print(temp[i]+" ");
        }

输出同样是stay hungry , stay foolish .

 

我们看看StringTokenizer的nextToken() 方法和String的split()方法有什么不同

 

先看StringTokenizer的nextToken() 方法

源码

public String nextToken() {
    /* 
     * If next position already computed in hasMoreElements() and
     * delimiters have changed between the computation and this invocation,
     * then use the computed value.
     */

    currentPosition = (newPosition >= 0 && !delimsChanged) ?  
        newPosition : skipDelimiters(currentPosition);

    /* Reset these anyway */
    delimsChanged = false;
    newPosition = -1;

    if (currentPosition >= maxPosition)
        throw new NoSuchElementException();
    int start = currentPosition;
    currentPosition = scanToken(currentPosition);
    return str.substring(start, currentPosition);
    }
 str.substring(start, currentPosition);

可以看出,最终是根据位置信息截取字符串的操作

 

再看和String的split()方法

    public String[] split(String regex) {
        return split(regex, 0);
    }

调用

    public String[] split(String regex, int limit) {
    return 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<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);
    }

根据正则表达式分割 过程就比较复杂了。

 

随笔系原创 转载请注明

欢迎访问我的博客

你可能感兴趣的:(StringTokenizer)