StringTokenizer 不能再J2ME中使用,可以这样替换

  StringTokenizer 不能再J2ME中使用,可以这样替换,达到类似效果~~~~~~~

 

  /**
     * Exists because StringTokenizer is not available in J2ME.
     * Returns an array with at least 1 entry.
     *
     * @param source must be non-null
     * @param delimiter
     * @return an array of Strings
     */
    public static String[] parseTokens(final String source, final char delimiter)
    {
        if (isEmpty(source))
        {
            return new String[0];
        }
       
        int numtoken = 1;
       
        for (int i = 0; i < source.length(); i++)
        {
            if (source.charAt(i) == delimiter)
            {
                numtoken++;
            }
        }
        String list[] = new String[numtoken];
        int nextfield = 0;
       
        for (int i = 0; i < numtoken; i++)
        {
            if (nextfield >= source.length())
            {
                list[i] = "";
            }
            else
            {
                int idx = source.indexOf(delimiter, nextfield);
                if (idx == -1)
                {
                    idx = source.length();
                }
                list[i] = source.substring(nextfield, idx);
                nextfield = idx + 1;
            }
        }
        return list;
    }

posted @ 2012-08-17 12:50  优秀程序缘  阅读(96)  评论(0编辑  收藏  举报