统计字符串中出现某次数的字符,如下语句,比如我要找出在str中出现3次的字符,应该怎么写。string str = 'abdcidslaaciejgjdsaqer';
private char[] GetChars(int length)
{
    //linq
}

解决方案 »

  1.   


                string str ="abdcidslaaciejgjdsaqer";
                foreach (char c in str)
                {
                    if (str.Count(p => p == c) == 3)
                    {
                    }
                }
      

  2.   


                string str ="abdcidslaaciejgjdsaqer";
                foreach (char c in str.Distinct())
                {
                    if (str.Count(p => p == c) == 3)
                    {
                    }
                }
      

  3.   

    string str = 'abdcidslaaciejgjdsaqer';
    private char[] GetChars(int length)
    {
       return str.ToCharArray().GroupBy(p => p).Where(p => p.Count() == length).Select(p=>p.Key).ToArray();
    }
      

  4.   

    string str = "abdcidslaaciejgjdsaqer";
    char[] t = str.ToCharArray();
    foreach (char c in t.Where(c1 => t.Count(c => c == c1) == 3).Distinct())
    {
        Console.WriteLine(c);
    }
      

  5.   


            Dim r = From c In str
                   Group By c
                   Into rg = Group, rc = Count()
                   Where rc = 3
      

  6.   

    string str = "abdcidslaaciejgjdsaqer";
    char[] arr = str.ToCharArray();
    var query =
      from a in arr
      group arr by a into g
      orderby g.Count()
      select new
      {
      g.Key,
      count = g.Count()
      };
      

  7.   


    void Main()
    {
    foreach(char c in GetChars(3))
    {
         Console.WriteLine(c);
    }
    }
    string str = "abdcidslaaciejgjdsaqer";
    private char[] GetChars(int length)
    {
    var query= from s in str.ToCharArray()
               group s by s into t
       where t.Count()==length
      select t.Key;
      return query.ToArray();
    }
    //结果:
    //d