C#如何统计出文本框中字母、空格、数字及其他字符的个数

C#如何统计出文本框中字母、空格、数字及其他字符的个数

使用ToCharArray方法即可。

源码如下:

 

private void num_count()
 {
 int ch = 0;//用来统计字母的数量
 int sp = 0;//用来统计空格的数量
 int math = 0; //用来统计数字的数量
 int other = 0;//用来统计其它字符的数量
 char[] c = textBox2.Text.ToCharArray();//把字符串转换成字符数组
 foreach (char i in c)
 {
 if (i >= 'a' && i = 'A' && i = '0' && i <= '9')
 math++;
 else if (i == ' ')
 sp++;
 else
 other++;
 }
 int totalnum = ch + math + sp + other;
 labeltext2.Text = totalnum.ToString();
 }

本文由查霆原创,原文地址:http://www.zhating.cn/index.php/post/56.html

你可能感兴趣的:(C#编程分享,C#编程常见问题集锦,C#,Csharp)