Vitty'S Blog

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  43 随笔 :: 221 文章 :: 215 评论 :: 0 Trackbacks

看到CSDN上有一个讨论筛选的问题,个人觉得不错,因此把它整理了一下,保存待用。

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

方法1:
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:
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里就是结果。


方法3:
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);

方法4:
   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

方法5:
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);
            }
        }
    }
}

posted on 2008-05-21 12:31 Vitty 阅读(464) 评论(0)  编辑 收藏 引用 所属分类: 1.电脑网络
只有注册用户登录后才能发表评论。