见以下代码中的GetChineseCharCount方法。这个方法对于英文+中文混合的字符有效。
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "1中2文3字4符,牛牛牛牛";
Console.WriteLine("中文字符数量为:{0}",
GetChineseCharCount(s));
}
static int GetChineseCharCount(string s)
{
int count = 0;
foreach(char c in s)
{
if (Convert.ToInt32(c) > 255)
{
count++;
}
}
return count;
}
}
}
方法