• 161031、java.util.StringTokenizer使用及源码


    import java.util.StringTokenizer;  
    public class TestStringTokenizer {   
        public static void main(String[] args) {  
            String ivrdata="v1|v2|你好|哈哈";  
            getTokenizer1(ivrdata);  
            getTokenizer2(ivrdata);  
            getTokenizer2Array(ivrdata,"|");   
            String ssss="y1     y2  split实现  哈哈";  
            String[] strarr=getTokenizer2Array(ssss,"");  
        }    
        public static void getTokenizer1(String str){  
            StringTokenizer st = new StringTokenizer(str, "|",true);  
            System.out.println("countTokens:"+st.countTokens());  
            int j=0;  
            while(st.hasMoreTokens()){  
                System.out.println(j+":"+st.nextToken());  
                j++;  
            }  
        }    
        public static void getTokenizer2(String str){  
            StringTokenizer st = new StringTokenizer(str, "|",false);  
            System.out.println("countTokens:"+st.countTokens());  
            int j=0;  
            while(st.hasMoreTokens()){  
                System.out.println(j+":"+st.nextToken());  
                j++;  
            }  
        }       
        /**以数组的形式获取*/
        public static String[] getTokenizer2Array(String str,String splitStr){  
            String[] arr=null;  
            if(str==null||str.trim().equals("")){   
            }else{  
                if(splitStr==null||splitStr.trim().equals("")){  
                    splitStr=" ";  
                }  
                StringTokenizer st = new StringTokenizer(str, splitStr,false);  
                System.out.println("ArraySize:"+st.countTokens());  
                arr=new String[st.countTokens()];  
                int j=0;  
                while(st.hasMoreTokens()){  
                    String temp=st.nextToken();  
                    System.out.println(j+":"+temp);  
                    arr[j]=temp;  
                    j++;  
                }  
            }  
            return arr;  
        }
    源码:
    package java.util;
    import java.lang.*;
    public class StringTokenizer implements Enumeration<Object> {
        private int currentPosition;//当前位置
        private int newPosition;//新位置
        private int maxPosition;//最大位置
        private String str;//操作的字符串
        private String delimiters;//分隔符
        private boolean retDelims;
        private boolean delimsChanged;
        private int maxDelimCodePoint;
        private boolean hasSurrogates = false;
        private int[] delimiterCodePoints;
        private void setMaxDelimCodePoint() {
            if (delimiters == null) {
                maxDelimCodePoint = 0;
                return;
            }
            int m = 0;
            int c;
            int count = 0;
            for (int i = 0; i < delimiters.length(); i += Character.charCount(c)) {
                c = delimiters.charAt(i);
                if (c >= Character.MIN_HIGH_SURROGATE && c <= Character.MAX_LOW_SURROGATE) {
                    c = delimiters.codePointAt(i);
                    hasSurrogates = true;
                }
                if (m < c)
                    m = c;
                count++;
            }
            maxDelimCodePoint = m;
            if (hasSurrogates) {
                delimiterCodePoints = new int[count];
                for (int i = 0, j = 0; i < count; i++, j += Character.charCount(c)) {
                    c = delimiters.codePointAt(j);
                    delimiterCodePoints[i] = c;
                }
            }
        }
        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);
        }
        public StringTokenizer(String str) {
            this(str, " 	
    
    f", false);
        }
        /**跳过分隔符*/
        private int skipDelimiters(int startPos) {
            if (delimiters == null)
                throw new NullPointerException();
            int position = startPos;
            while (!retDelims && position < maxPosition) {
                if (!hasSurrogates) {
                    char c = str.charAt(position);
                    if ((c > maxDelimCodePoint) || (delimiters.indexOf(c) < 0))
                        break;
                    position++;
                } else {
                    int c = str.codePointAt(position);
                    if ((c > maxDelimCodePoint) || !isDelimiter(c)) {
                        break;
                    }
                    position += Character.charCount(c);
                }
            }
            return position;
        }
        /**从某个位置开始遍历token*/
        private int scanToken(int startPos) {
            int position = startPos;
            while (position < maxPosition) {
                if (!hasSurrogates) {
                    char c = str.charAt(position);
                    if ((c <= maxDelimCodePoint) && (delimiters.indexOf(c) >= 0))
                        break;
                    position++;
                } else {
                    int c = str.codePointAt(position);
                    if ((c <= maxDelimCodePoint) && isDelimiter(c))
                        break;
                    position += Character.charCount(c);
                }
            }
            if (retDelims && (startPos == position)) {
                if (!hasSurrogates) {
                    char c = str.charAt(position);
                    if ((c <= maxDelimCodePoint) && (delimiters.indexOf(c) >= 0))
                        position++;
                } else {
                    int c = str.codePointAt(position);
                    if ((c <= maxDelimCodePoint) && isDelimiter(c))
                        position += Character.charCount(c);
                }
            }
            return position;
        }
        /**判断该位置字符是否是分隔符*/
        private boolean isDelimiter(int codePoint) {
            for (int i = 0; i < delimiterCodePoints.length; i++) {
                if (delimiterCodePoints[i] == codePoint) {
                    return true;
                }
            }
            return false;
        }
        /**是否还有token*/
        public boolean hasMoreTokens() {
            newPosition = skipDelimiters(currentPosition);
            return (newPosition < maxPosition);
        }
        public String nextToken() {
            currentPosition = (newPosition >= 0 && !delimsChanged) ?
                newPosition : skipDelimiters(currentPosition);
            delimsChanged = false;
            newPosition = -1;
            if (currentPosition >= maxPosition)
                throw new NoSuchElementException();
            int start = currentPosition;
            currentPosition = scanToken(currentPosition);
            return str.substring(start, currentPosition);
        }
        /**下一个token*/
        public String nextToken(String delim) {
            delimiters = delim;
            delimsChanged = true;
            setMaxDelimCodePoint();
            return nextToken();
        }
        /**是否还有元素*/
        public boolean hasMoreElements() {
            return hasMoreTokens();
        }
        /**下一个元素*/
        public Object nextElement() {
            return nextToken();
        }
        /**计算Tokens的数量*/
        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;
        }
    }
  • 相关阅读:
    ibatis学习笔记
    记ibatis使用动态列查询问题(remapresults)
    jQuery(九)、ajax对象操作
    jQuery(八)、ajax
    jQuery(七)、效果和动画
    jQuery(六)、事件
    jQuery(五)、筛选
    jQuery(四)、文档处理
    jQuery(三)、属性、CSS
    jQuery(二)、选择器
  • 原文地址:https://www.cnblogs.com/zrbfree/p/6023880.html
Copyright © 2020-2023  润新知