您当前的位置: 首页 >  c#
  • 4浏览

    0关注

    193博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#判断中文和英文字符长度

我寄人间雪满头丶 发布时间:2020-07-02 10:29:19 ,浏览量:4

string如果直接获取Length是无法区分中英文还有字符之间的区别。以下方法可以识别中文长度为2,英文字符为1。


class Program
    {
        static void Main(string[] args)
        {
        	//长度为5
            GetStrLength("嘻嘻x");
            Console.ReadKey();
        }
 
 		//获取长度方法
        private static int GetStrLength(string str)
        {
            if (string.IsNullOrEmpty(str)) return 0;
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            byte[] s = ascii.GetBytes(str);
            for (int i = 0; i             
关注
打赏
1648518768
查看更多评论
0.1886s