我有一个字符串abcd45612,asd我想用一个程序来判断有几个字母,几个数字,几个标点?谢谢

解决方案 »

  1.   

    比较笨的方法,判断ASCII码:            string s = "abcd45612,asd";
                int characters = 0;
                int numbers = 0;
                int symbols = 0;
                foreach (char c in s)
                {
                    if ((c >= 33 && c <= 47) || (c >= 58 && c <= 64) || (c >= 91 && c <= 96) || (c >= 123 && c <= 126))
                        symbols++;
                    if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122))
                        characters++;
                    if (c >= 48 && c <= 57)
                        numbers++;
                }
                Console.WriteLine("共有{0}个字母,{1}个数字,{2}个标点", characters, numbers, symbols);
      

  2.   

    将字符串转换为 char类型的数组char[] operator = string.ToCharArray()
    for(int i = 0 ;i < operator.Lenght;i++)
    {
             operator[i].isDigit//是否为十进制数字isNumber 相关的方法去看Char类
    }
      

  3.   

    根据2楼提供的思路修改了一下:            string s = "abcd45612,asd";
                int characters = 0;
                int numbers = 0;
                int symbols = 0;
                foreach (char c in s)
                {
                    
                    if(char.IsPunctuation(c))
                        symbols++;
                    if(Char.IsLetter(c))
                        characters++;
                    if(char.IsDigit(c))
                        numbers++;
                }
                Console.WriteLine("共有{0}个字母,{1}个数字,{2}个标点", characters, numbers, symbols);
      

  4.   

    我是想在一个页面上加一个textbox和一个按钮button,点击button之后获取textbox的值同时加以判断,我试了一下,加在button事件里好像没有反应?
      

  5.   

    winform or webform?
    winform:
    MessageBox.Show(string.Format("共有{0}个字母,{1}个数字,{2}个标点", characters, numbers, symbols));webform:
    Reponse.Write(string.Format("共有{0}个字母,{1}个数字,{2}个标点", characters, numbers, symbols));
      

  6.   

    就在csdn上给我发消息或者在我空间留言都可以,
    我上csdn比较多