关于在一段字符串中提取所有两个字符中间的字符串

关于在一段字符串中提取所有两个字符中间的字符串

这里的提取无非也就是自己写了一个方便自己使用的方法,这个方法里面用到了递归和ref,而使用ref的时候必须得先在调用该方法之前初始化一个值,在调用完该方法以后,带有ref前缀的变量的值就是调用方法完了以后变成的值了!!

/// <summary>
        /// 把两个字符中间的字符提取出来
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="a">第一个字符</param>
        /// <param name="b">第二个字符</param>
        /// <param name="le">第一个字符初始开始数的索引</param>
        /// <param name="ri">第二个字符初始开始数的索引</param>
        /// <param name="list">提取出来的字符组成的集合</param>
        public void getArryList(string str, char a, char b, int le, int ri, ref List<string> list)
        {
            int left = le;
            int right = ri;
            int one = str.IndexOf('[', left + 1);
            int two = str.IndexOf(']', right + 1);
            left = one;
            right = two;
            if (one >= 0 && two >= 0)
            {
                list.Add(str.Substring(one + 1, right - (left + 1)));
                int i = str.Length;
                int qq = str.LastIndexOf('[');
                int ii = str.LastIndexOf(']');
                if (left != str.LastIndexOf('[') && right != str.LastIndexOf(']'))
                {
                    getArryList(str, a, b, left, right, ref list)   ;


                }

              }
        }

下面是如何去使用上面的方法

List<string> list = new List<string>();;

string code="[1]表示教师[2]表示学生";

getArryList(code, '[', ']', 0, 0, ref list);

调用完方法以后字符串的集合就有1和2了

posted @ 2016-12-26 14:53  jeremy1888  阅读(4788)  评论(0编辑  收藏  举报