N156,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164, 怎么取到2!!就是相同的算一个 

解决方案 »

  1.   


                string s = "N156,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,";
                string[] array = s.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);            int count = array.Distinct().Count();
      

  2.   

                string str = @"N156,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,";
                while (str != (str = Regex.Replace(str, @"(([^,]+?,)([^,]*?,)*?)\2", "$1"))) ;
                Console.WriteLine(str);
    //N156,N164,
      

  3.   


                string s = @"N156,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,";            Regex reg = new Regex(@"(?<=([^,]+?,)).*?(\1)");
                string dist = reg.Replace(s, "");            int count = dist.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Length;
      

  4.   

      string str = "N156,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164";
            Response.Write(str.Split(',').ToList().Distinct().Count().ToString());
      

  5.   


    恩 的确,这个还是用linq直接
      

  6.   


                string str = @"N156,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,";            Regex reg = new Regex(@"(?<=([^,]+,))(.*?)\1");
                while (str != (str = reg.Replace(str,"$2")));            int count = str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Length;这个应该对了吧
      

  7.   

    要不就用循环处理,先把字符串,转行成数组,再匹配,将没有出现过的数组中的放到另外一个数据里,则也可以达到效果,不过这种方法,比较笨,建议还是用集合的distinct方法!
      

  8.   

                string str = "N156,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,N164,";
                string[] items = str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var count = items.Distinct().Count();
                Console.WriteLine(count);