对于我有这样一个字符串 str="sin(b2 * 3)+cos(b4)+b4*0.05+b1*b3-b3*0.04"
以下代码可以完成此功能:可以从上面字符串中 提出b2 b4 b4 b1 b3 b3这样子串,就是b加后面编号;
              Regex re = new Regex(@"b\d+");
            ModelExpression = Txt_ModelStr.Text.Trim();
            if (re.IsMatch(ModelExpression))
            {
                Match BandList = re.Match(ModelExpression);
                while (BandList.Success)
                {
                   MessageBox.Show(BandList.Value.ToString());
                   BandList = BandList.NextMatch();
                }
            }
现在我想要做的事筛选出其中重复的子串并删除重复的 ,处理后应该像这样 b2 b4 b1 b3(第一个问题)!
再次我想根据b的下标从小到大进行排序,那么得到结果应该是 b1 b2 b3 b4(第二个问题)!
并将b1 b2 b3 b4 存到一数组中!Arraylist[0]="b1",Arraylist[1]="b2"......
谢谢各位!

解决方案 »

  1.   

    先排序,然后判断相邻项,定义一个List<string> 变量如果不等就添加进去
    http://space.itpub.net/221062/viewspace-481733
      

  2.   


    HashSet<string> set = new HashSet<string>();
                foreach (DataRow r in table.Rows)
                {
                    set.Add("xxxx");//放到hashset可以过滤重复值
                }            ArrayList list = new ArrayList();//把hashset的值放到list,利用list排序
                foreach (string s in set)
                {
                    list.Add(s);
                }
                list.Sort(new MyCompare());private class MyCompare : System.Collections.IComparer//定义自己的排序规则
            {
                public int Compare(object x, object y)
                {
                    string s1 = (string)x;
                    string s2 = (string)y;
                    int year1 = int.Parse(s1.Substring(0, s1.IndexOf("年")));//这里你可以改成自己的比较,比如截取b以后的string转成int,返回两个int的差值
                    int year2 = int.Parse(s2.Substring(0, s2.IndexOf("年")));
                    int m1 = int.Parse(s1.Substring(s1.IndexOf("年") + 1, 2));
                    int m2 = int.Parse(s2.Substring(s2.IndexOf("年") + 1, 2));
                    int i =  (year1-year2)*12+m1-m2;
                    return -i;
                }
            
      

  3.   

    先将这些值都加到ArrayList.
    ArrayList arraylist = new ArrayList();
                    arraylist.AddRange(new string[] { "b3", "b4", "b3", "b2" });
                    arraylist.Sort();
                    for (int i = 1; i < arraylist.Count; i++)
                        if (arraylist[i] == arraylist[i - 1])
                            arraylist.RemoveAt(i--);
      

  4.   

    try:            string str = "sin(b2 * 3)+cos(b4)+b4*0.05+b1*b3-b3*0.04";
                List<string> list = new List<string>();
                foreach(Match match in Regex.Matches(str,@"b\d+"))
                    if(!list.Contains(match.Value))
                        list.Add(match.Value);
                list.Sort(new Comparison<string>(delegate(string s1, string s2)
                    {
                        int a1 = int.Parse(s1.Replace("b", ""));
                        int a2 = int.Parse(s2.Replace("b", ""));
                        return a1.CompareTo(a2);
                    }));
                foreach (string s in list)
                    Console.WriteLine(s);
    /*
    输出:
    b1
    b2
    b3
    b4
    */
    如果楼主用的是vs2008,那么2楼的HashSet是个不错选择
      

  5.   

    上面都说了。用list<string>排序就可
      

  6.   

                string csdnstr = "sin(b2 * 3)+cos(b4)+b4*0.05+b1*b3-b3*0.04";
                string csdnpattern = "b[0-9]*";
                Regex csdnregex = new Regex(csdnpattern);
                MatchCollection csdnmatch = csdnregex.Matches(csdnstr);
                List<string> csdnd = new List<string>();
                foreach (Match m in csdnmatch)
                {
                    csdnd.Add(m.Value.ToString());
                }
                csdnd.Sort();
                for (int i = csdnd.Count-1  ;i>0 ;i--)
                {
                    if (csdnd[i] == csdnd[i - 1])
                        csdnd.RemoveAt(i);
                }
                foreach (string str in csdnd)
                {
                    Console.WriteLine(str);
                }
                Console.ReadLine();
      

  7.   

    ArrayList
    ArrayList arraylist = new ArrayList();
                    arraylist.AddRange(new string[] { "b3", "b4", "b3", "b2" });
                    arraylist.Sort();
                    for (int i = 1; i < arraylist.Count; i++)
                        if (arraylist[i] == arraylist[i - 1])
                            arraylist.RemoveAt(i--);