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

  • 相关阅读:
    linux驱动开发学习一:创建一个字符设备
    如何高效的对有序数组去重
    找到缺失的第一个正整数
    .NET不可变集合已经正式发布
    中国人唯一不认可的成功——就是家庭的和睦,人生的平淡【转】
    自己动手搭建 MongoDB 环境,并建立一个 .NET HelloWorld 程序测试
    ASP.NET MVC 中如何用自定义 Handler 来处理来自 AJAX 请求的 HttpRequestValidationException 错误
    自己动手搭建 Redis 环境,并建立一个 .NET HelloWorld 程序测试
    ServiceStack 介绍
    一步一步实战扩展 ASP.NET Route,实现小写 URL、个性化 URL
  • 原文地址:https://www.cnblogs.com/ShineTan/p/2554674.html
Copyright © 2011-2022 走看看