如下是为统计标点符号、字母、汉字、数字的一段小程序。但是不知为什么,当输入:KH中 时,即显示汉字的个数为:0的。。还请高手,指教。。不知是不是小弟的正则那里出了问题。小弟无尽感激 。
小弟想要的结果是:无论输入一段怎样的小文章,都能统计出文章里面的符号、字母、汉字、数字。的个数。。
请高手们帮帮 小弟,小弟在这里先感激各位高手们先了。 class Program
{
static void Main(string[] args)
{ Console.Write("请输入文本:");
string text=Console.ReadLine();
cuang(text);
}
static void cuang(string text)
{
Regex regex = new Regex("[\u4E00-\u9FA5]{0,}");
string num = "0123456789";
string mu = "asdfghjklqwertyuiopzxcvbnm";
string biao = ",.“”!/、〔〕-·@#¥%……&*()+=";
int nu=0;
int m=0;
int bia=0;
int other=0;
int kong=0;
int han=0;
int len = text.Length;
string caeer = "";
MatchCollection mm = regex.Match(text);//运行时显示这里错了。但是小弟不知怎样改,还请高手帮帮小弟
han = mm.Count;
for (int i = 0; i < len; i++)
{
caeer = text.Substring(i, 1);
if (num.IndexOf(caeer) != -1)
{
nu = 1 + nu;//数字的
}
else if (mu.IndexOf(caeer) != -1)
{
m += 1;//字母的
}
else if (mu.ToUpper().IndexOf(caeer) != -1)
{
m += 1;//字母 的
}
else if (biao.IndexOf(caeer) != -1)
{
bia += 1;//标点的
}
else if (caeer == " ")
{
kong += 1;
}
else
{
other += 1;
}
}
Console.Write("数字的个数为:{0}\n", nu);
Console.Write("字母的个数为:{0}\n", m);
Console.Write("标点符号的个数为:{0}\n", bia);
Console.Write("汉字的个数为:{0}\n", han);
Console.Write("其他的个数为:{0}\n", other);
}
}
}
小弟想要的结果是:无论输入一段怎样的小文章,都能统计出文章里面的符号、字母、汉字、数字。的个数。。
请高手们帮帮 小弟,小弟在这里先感激各位高手们先了。 class Program
{
static void Main(string[] args)
{ Console.Write("请输入文本:");
string text=Console.ReadLine();
cuang(text);
}
static void cuang(string text)
{
Regex regex = new Regex("[\u4E00-\u9FA5]{0,}");
string num = "0123456789";
string mu = "asdfghjklqwertyuiopzxcvbnm";
string biao = ",.“”!/、〔〕-·@#¥%……&*()+=";
int nu=0;
int m=0;
int bia=0;
int other=0;
int kong=0;
int han=0;
int len = text.Length;
string caeer = "";
MatchCollection mm = regex.Match(text);//运行时显示这里错了。但是小弟不知怎样改,还请高手帮帮小弟
han = mm.Count;
for (int i = 0; i < len; i++)
{
caeer = text.Substring(i, 1);
if (num.IndexOf(caeer) != -1)
{
nu = 1 + nu;//数字的
}
else if (mu.IndexOf(caeer) != -1)
{
m += 1;//字母的
}
else if (mu.ToUpper().IndexOf(caeer) != -1)
{
m += 1;//字母 的
}
else if (biao.IndexOf(caeer) != -1)
{
bia += 1;//标点的
}
else if (caeer == " ")
{
kong += 1;
}
else
{
other += 1;
}
}
Console.Write("数字的个数为:{0}\n", nu);
Console.Write("字母的个数为:{0}\n", m);
Console.Write("标点符号的个数为:{0}\n", bia);
Console.Write("汉字的个数为:{0}\n", han);
Console.Write("其他的个数为:{0}\n", other);
}
}
}
我觉得
你这个功能根本就不需要用到正则表达式去匹配啊。
如果你这样写的话
caeer是记录在循环的时候文本中的字符
然后判断这个字符在num mu biao 其中是否存在的。用string.index()函数就可以了不过话说回来你的标点应该还分圆角和半角
字符还分大写和小写
那样的话
你可以直接判断asc码
那样会更快
直接转为char[],或直接读取text[i],然后根据值做判断就行了
基本上就是这样,细节再调整一下吧!using System;namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
string str = "我爱北京天安门 0123456789 asdfghjklqwertyuiopzxcvbnm ,.“”!/、〔〕-·@#¥%……&*()+=";
Stat(str);
} static void Stat(string text)
{
int num = 0,letter = 0,cnChar = 0, symbol = 0; for (int i = 0; i < text.Length; i++)
{
if (text[i] >= 0x4e00 && text[i] <= 0x9fff)
cnChar++;
else if (text[i] >= '0' && text[i] <= '9')
num++;
else if (text[i] >= 'a' && text[i] <= 'z')
letter++;
else if (text[i] >= 'A' && text[i] <= 'Z')
letter++;
else if (text[i] < 256)
symbol++;
}
}
}
}