现在我想检验字符串 里面有25个字符。都是随机的010111010这类的。我想检验里面有多少个1.如果超出5个1就把这个字符串删除。请高手帮忙~

解决方案 »

  1.   


    int i = 0;
    string str = "101010101010101010";
    foreach(char c in str)
    {
      if(c.Equals('1'))
         i ++;
    }
    //查看有多少个1
    Console.WriteLine("共有" + i + "个1");
      

  2.   

    string str = "101010101010101010";
    int i=(from c in str
           where c=='1'
           select c).Count();if(i>5)......
      

  3.   


                string str = "111010101010101010101011010101010111";
                int x = str.Split('1').Length;
                bool IsDelete = false;
                if (x >= 6)
                    IsDelete = true;另类一下。
      

  4.   

    哈哈 这类问题,楼上 说得很明白了,一个foreach 循环就可以了,楼主加油