大家好,我在书上看到一个关于“判断一串字符中有多少个汉字”的程序,想跟大家分享一下:
主要使用正则表达式来解决的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace 判断汉字的个数
{
class Program
{
static void Main(string[] args)
{
int count = 0;
string str = "你好 mr.right";
Regex ex = new Regex("^[\u4e00-\u9fa5]{0,}$");
for (int i = 0; i < str.Length; i++)
{
if (ex.Match(str[i].ToString()).Success)
{
count++;
}
}
Console.WriteLine(count);
Console.Read();
}
}
}