如何判断一个字符串中包含几个字母,几个数字,几个标点?

比较笨的方法,判断ASCII码:

C# code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

            string s = "abcd45612,asd";

            int characters = 0;

            int numbers = 0;

            int symbols = 0;

            foreach (char 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);

猜你喜欢

转载自blog.csdn.net/u014644594/article/details/82775912
今日推荐