如何判断C#字符串里中文字符有几个

如题所述

见以下代码中的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;
        }
    }
}


方法

温馨提示:答案为网友推荐,仅供参考
相似回答