假如有一字符串(001,002,001,003,002,001,003)如何能筛选出相同的,最后只得到001,002,003

解决方案 »

  1.   

    嘿嘿 如果用linq的话..using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO;
    using System.Net;
    using System.Linq;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main()
            {
                string str = "(001,002,001,003,002,001,003)";
                str = str.Substring(1, str.Length - 2);
                var result=from s in str.Split(',') group s by s into g select g.Key;
                foreach (var g in result) {
                    Console.WriteLine(g);
                }
            }
        }
    }
      

  2.   

    linq都用上了,佩服3楼的。只能vs2008才能用哦。
      

  3.   

    string str = "(001,002,001,003,002,001,003)";
                str = str.Substring(1, str.Length - 2);
                string[] strA = str.Split(new char[]{','});
                ArrayList al = new ArrayList();
                foreach (string str1 in strA)
                {
                    if (!al.Contains(str1)) al.Add(str1);
                }al里就是你要的东西。
      

  4.   

     string ss = "001,002,001,003,002,001,003";
                string[] arr = ss.Split(',');
                string reVal = string.Empty;
                for (int i = 0; i < arr.Length; i++)
                {
                    if (!reVal.Contains(arr[i]))
                    {
                        reVal += arr[i]+",";
                    }
                }
                reVal = reVal.Substring(0, reVal.Length - 1);
      

  5.   

       private void comboBoxGroupSortView_DropDown(object sender, EventArgs e)
            {
                ////当combobox弹出DropDown下拉框时,将 olvColumnGroupID得组加入到下拉列表,并添加 All选项
                if (cmd.ListObjectPlayer.Count > 0)
                {
                    List<string> GroupsStr = new List<string>();
                    GroupsStr.Add("All");
                    for (int n = 0; n < cmd.ListObjectPlayer.Count; n++)
                    {
                        GroupsStr.Add(cmd.ListObjectPlayer[n].GroupID);
                    }
                    GroupsStr.Sort();
                    List<String> Groupsorter = new List<string>();
                    for (int gs = 0; gs < GroupsStr.Count; gs++)
                    {
                        if (gs == 0)
                        {
                            Groupsorter.Add(GroupsStr[gs]);
                        }
                        else if (GroupsStr[gs] != GroupsStr[gs - 1])
                        {
                            Groupsorter.Add(GroupsStr[gs]);
                        }
                    }
                    Groupsorter.Sort();
                    comboBoxGroupSortView.Items.Clear();
                    for (int s = 0; s < Groupsorter.Count; s++)
                    {
                        comboBoxGroupSortView.Items.Add(Groupsorter[s]);
                    }
                }
            }
    ListObjectPlayer是List<Player> 数组对象 ,Player有 GroupID,PlayerID,MAC等字段
    在Objectlistview一个对象的listview控件里有你说的(001,002,001,003,002,001,003)组列,把这个加到comboBoxGroupSortView里
    显示
    001,002,003,all
      

  6.   

    using System.Collection;
    using System;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main()//这是我认为的效率最高的一种
            {
                string ss = "001,002,001,003,002,001,003";
                HashTable ht = new HashTable();
                string[] temp = ss.Split(',');
                foreach(string s in temp)
                {
                    ht[s] = s;
                }
                foreach(string s in ht.Keys)
                {
                    Console.WriteLine(s);
                }
            }
        }
    }
      

  7.   

    9楼的思路巧妙,不过ht[s] = s这行有问题吧,应该是ht.add(s,"ssss")才对,后面的字符串值可以随便赋。
      

  8.   

    没问题的,我试过的。你说的也没错,因为HashTable有重载操作符 = 的。