zoukankan      html  css  js  c++  java
  • 一天一个类 --- StringTokenizer

    这是一个将字符串按照指定的delimiters(分隔符)进行分割的类。

    首先看看他的构造函数:

        public StringTokenizer(String str, String delim, boolean returnDelims) {
            currentPosition = 0;
            newPosition = -1;
            delimsChanged = false;
            this.str = str;
            maxPosition = str.length();
            delimiters = delim;
            retDelims = returnDelims;
            setMaxDelimCodePoint();
        }
        public StringTokenizer(String str, String delim) {
            this(str, delim, false);
        }

    默认是不返回delimiters的。

      public StringTokenizer(String str) { this(str, " f", false); } 

    这个是个默认的构造函数,其中使用的默认的delimiter是 " f"

    2、遍历边界 

        public boolean hasMoreTokens() {
            /*
             * Temporarily store this position and use it in the following
             * nextToken() method only if the delimiters haven't been changed in
             * that nextToken() invocation.
             */
            newPosition = skipDelimiters(currentPosition);
            return (newPosition < maxPosition);
        }

    查看是不是还有其他的分割串,这个通常作为遍历这个字符串是否结束的判断条件

    3、获取每一个分割串

     1     public String nextToken() {
     2         /*
     3          * If next position already computed in hasMoreElements() and
     4          * delimiters have changed between the computation and this invocation,
     5          * then use the computed value.
     6          */
     7 
     8         currentPosition = (newPosition >= 0 && !delimsChanged) ?
     9             newPosition : skipDelimiters(currentPosition);
    10 
    11         /* Reset these anyway */
    12         delimsChanged = false;
    13         newPosition = -1;
    14 
    15         if (currentPosition >= maxPosition)
    16             throw new NoSuchElementException();
    17         int start = currentPosition;
    18         currentPosition = scanToken(currentPosition);
    19         return str.substring(start, currentPosition);
    20     }

    上面最后一句代码可以看出,使用的是字符串的一些操作,就是返回相应的字串。

    这里还有一个特殊的方法,返回指定delimiter分割串

        public String nextToken(String delim) {
            delimiters = delim;
    
            /* delimiter string specified, so set the appropriate flag. */
            delimsChanged = true;
    
            setMaxDelimCodePoint();
            return nextToken();
        }

    4、查看该字串串被分为了多少个字串

        public int countTokens() {
            int count = 0;
            int currpos = currentPosition;
            while (currpos < maxPosition) {
                currpos = skipDelimiters(currpos);
                if (currpos >= maxPosition)
                    break;
                currpos = scanToken(currpos);
                count++;
            }
            return count;
        }

    此时,如果没有指定delimiter的话,将会返回0

  • 相关阅读:
    [Swift]LeetCode910. 最小差值 II | Smallest Range II
    转 关于shell脚本中#!/bin/bash and #!/bin/ksh 的说明
    转 对象继承
    转 PHP编程过程中需要了解的this,self,parent的区别
    转: ORA-06508 could not find program unit being called: "DBSNMP.BSLN_INTERNAL
    Multitenant best Practice clone pdb seed and Clone a Pluggable Database – 12c Edition
    Plugging an Unplugged Pluggable Database issue 3
    日历 php
    Datapatch AND What to do if the status of a datapatch action was not SUCCESS due to finding non-ignorable errors
    oracle中的用户详解 【转】
  • 原文地址:https://www.cnblogs.com/plxx/p/4533246.html
Copyright © 2011-2022 走看看