zoukankan      html  css  js  c++  java
  • 【摘要】一个字符串解析类

    很久以前在CodeProject上看到一个控件源码http://www.codeproject.com/Articles/7247/Themed-Windows-XP-style-Explorer-Bar,里面有一个字符串解析的类,今天翻出来贴到这里。

    #region StringTokenizer Class

        /// <summary>
        /// A class that breaks a string into tokens
        /// </summary>
        internal class StringTokenizer
        {
            /// <summary>
            /// The index of the current token
            /// </summary>
            private int currentIndex;

            /// <summary>
            /// The number of tokens
            /// </summary>
            private int numberOfTokens;

            /// <summary>
            /// Internal list of tokens
            /// </summary>
            private ArrayList tokens;

            /// <summary>
            /// The string to be parsed
            /// </summary>
            private string source;

            /// <summary>
            /// The delimiters
            /// </summary>
            private string delimiter;

            /// <summary>
            /// Initializes a new instance of the StringTokenizer class with the
            /// specified source string and delimiters
            /// </summary>
            /// <param name="source">The String to be parsed</param>
            /// <param name="delimiter">A String containing the delimiters</param>
            public StringTokenizer(string source, string delimiter)
            {
                this.tokens = new ArrayList(10);
                this.source = source;
                this.delimiter = delimiter;

                if (delimiter.Length == 0)
                {
                    this.delimiter = " ";
                }

                this.Tokenize();
            }

            /// <summary>
            /// Initializes a new instance of the StringTokenizer class with the
            /// specified source string and delimiters
            /// </summary>
            /// <param name="source">The String to be parsed</param>
            /// <param name="delimiter">A char array containing the delimiters</param>
            public StringTokenizer(string source, char[] delimiter)
                : this(source, new string(delimiter))
            {

            }

            /// <summary>
            /// Initializes a new instance of the StringTokenizer class with the
            /// specified source string
            /// </summary>
            /// <param name="source">The String to be parsed</param>
            public StringTokenizer(string source)
                : this(source, "")
            {

            }

            /// <summary>
            /// Parses the source string
            /// </summary>
            private void Tokenize()
            {
                string s = this.source;
                StringBuilder sb = new StringBuilder();
                this.numberOfTokens = 0;
                this.tokens.Clear();
                this.currentIndex = 0;

                int i = 0;

                while (i < this.source.Length)
                {
                    if (this.delimiter.IndexOf(this.source[i]) != -1)
                    {
                        if (sb.Length > 0)
                        {
                            this.tokens.Add(sb.ToString());

                            sb.Remove(0, sb.Length);
                        }
                    }
                    else
                    {
                        sb.Append(this.source[i]);
                    }

                    i++;
                }

                this.numberOfTokens = this.tokens.Count;
            }

            /// <summary>
            /// Returns the number of tokens in the string
            /// </summary>
            /// <returns>The number of tokens in the string</returns>
            public int CountTokens()
            {
                return this.tokens.Count;
            }

            /// <summary>
            /// Checks if there are more tokens available from this tokenizer's
            /// string
            /// </summary>
            /// <returns>true if more tokens are available, false otherwise</returns>
            public bool HasMoreTokens()
            {
                if (this.currentIndex <= (this.tokens.Count - 1))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            /// <summary>
            /// Returns the current token and moves to the next token
            /// </summary>
            /// <returns>The current token</returns>
            public string NextToken()
            {
                string s = "";

                if (this.currentIndex <= (this.tokens.Count - 1))
                {
                    s = (string)tokens[this.currentIndex];

                    this.currentIndex++;

                    return s;
                }
                else
                {
                    return null;
                }
            }

            /// <summary>
            /// Moves to the next token without returning the current token
            /// </summary>
            public void SkipToken()
            {
                if (this.currentIndex <= (this.tokens.Count - 1))
                {
                    this.currentIndex++;
                }
            }

            /// <summary>
            /// Returns the current token but does not move to the next token
            /// </summary>
            /// <returns></returns>
            public string PeekToken()
            {
                string s = "";

                if (this.currentIndex <= (this.tokens.Count - 1))
                {
                    s = (string)tokens[this.currentIndex];

                    return s;
                }
                else
                {
                    return null;
                }
            }

            /// <summary>
            /// Returns the source string
            /// </summary>
            public string Source
            {
                get
                {
                    return this.source;
                }
            }

            /// <summary>
            /// Returns a string that contains the delimiters used to
            /// parse the source string
            /// </summary>
            public string Delimiter
            {
                get
                {
                    return this.delimiter;
                }
            }
        }

        #endregion

  • 相关阅读:
    【Django】django 处理request流程细节(转)
    【Django】request 处理流程(转)
    【Django】中间件
    【HTML】DocType
    【CSS】伪类与伪元素
    【nodejs】nodejs 的linux安装(转)
    【chrome】 chrome 开发者工具
    【samba】samba 用户权限配置(转)
    form表单组件
    自定义分页器
  • 原文地址:https://www.cnblogs.com/ShineTan/p/2554674.html
Copyright © 2011-2022 走看看