C# 计算字符串的长度

String类型的字符,用length来获取长度是不区分中文和英文的

我们按中文两个字符,英文一个字符来计算String字符的长度:

 public static int StrLength(string inputString)
        {
            System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
            int tempLen = 0;
            byte[] s = ascii.GetBytes(inputString);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                    tempLen += 2;
                else
                    tempLen += 1;
            }
            return tempLen;
        }

此文转载自C#计算字符串长度,汉字算两个字符_IT技术小趣屋

原文地址:https://www.cnblogs.com/liuzheng0612/p/12844038.html