打比方说就是求string s = "adAwSD?asd fds a45a3sd?sad23 fwds";这个字符串中:?号,空格,数字,字母(不分大小写)的个数????????

解决方案 »

  1.   

    怎么求一个数字在字符串中出现的个数,仿佛可以通过字母的ASC11码来做,具体怎么做,现在在网上搜不找了
      

  2.   

    如果只是统计数字和字母的个数很容易int dCount = 0;  //数字
    int cCount = 0;  //字母
    string s = "adAwSD?asd fds a45a3sd?sad23 fwds";
    foreach (char c in s)
    {
        if (c >= '0' && c <= '9')
            dCount++;
        else if (char.ToLower(c) >= 'a' && char.ToLower(c) <= 'z')
            cCount++;
    }如果统计其它的,那就看你的规则了,是单独统计,还是说除这两种字符之外,放在一起统计
      

  3.   

    谢谢lxcnn(过客)的指点空格 和  符号 要怎么办啊?
      

  4.   

    单独统计就在后面加if分支就行了,比如统计空格int dCount = 0;
    int cCount = 0;
    int spaceCount = 0;
    string s = "adAwSD?asd fds a45a3sd?sad23 fwds";
    foreach (char c in s)
    {
        if (c >= '0' && c <= '9')
            dCount++;
        else if (char.ToLower(c) >= 'a' && char.ToLower(c) <= 'z')
            cCount++;
        else if (c == ' ')
            spaceCount++;
    }