java java中subString、split、stringTokenizer三种截取字符串方法的性能比较

都知道在大数据量情况下,使用String的split截取字符串效率很低,这种时候可以用substring,stringTokenizer
代替,效率会有一定的提高,而stringTokenizer中实际是通过调用substring实现的。


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


想要查看测试结果可以看这篇博客 点击打开链接 忽略其中的stringTokenizer部分。stringTokenizer应该并不会比substring高效。而且StringTokenizer是出于兼容性的原因而被保留的遗留类(在新代码中并不鼓励使用它)

你可能感兴趣的:(java)