C#中的String.Length获取中文字符串长度出错

项目需要截取中文字符,中文字符大于255的时候,需要截取字符,在这里出现了问题。因为使用的是String.length。

Length 属性返回此实例中 Char 对象的个数,而不是 Unicode 字符个数。 原因在于一个 Unicode 字符可能会用多个 Char 表示。 使用System.Globalization.StringInfo 类来处理每个 Unicode 字符而不是每个 Char

附上获取中文字符长度,并截取的类的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
   public static class SplitWorld
    {
        /// <summary>
        /// 获取中英文混排字符串的实际长度(字节数)
        /// </summary>
        /// <param name="str">要获取长度的字符串</param>
        /// <returns>字符串的实际长度值(字节数)</returns>
        public static int Length(string str)
        {
            if (str.Equals(string.Empty))
                return 0;
            int strlen = 0;
            ASCIIEncoding strData = new ASCIIEncoding();
            //将字符串转换为ASCII编码的字节数字
            byte[] strBytes = strData.GetBytes(str);
            for (int i = 0; i <= strBytes.Length - 1; i++)
            {
                if (strBytes[i] == 63)  //中文都将编码为ASCII编码63,即"?"号
                    strlen++;
                strlen++;
            }
            return strlen;
        }

        /// <summary>截取指定字节长度的字符串</summary> 
        /// <param name="str">原字符串</param>
        ///<param name="len">截取字节长度</param> 
        /// <returns>string</returns>
        public static string SubString(string str, int len)
        {
            string result = string.Empty;// 最终返回的结果
            if (string.IsNullOrEmpty(str))
            {
                return result;
            }
            int byteLen = System.Text.Encoding.Default.GetByteCount(str);
            // 单字节字符长度
            int charLen = str.Length;
            // 把字符平等对待时的字符串长度
            int byteCount = 0;
            // 记录读取进度 
            int pos = 0;
            // 记录截取位置 
            if (byteLen > len)
            {
                for (int i = 0; i < charLen; i++)
                {
                    if (Convert.ToInt32(str.ToCharArray()[i]) > 255)
                    // 按中文字符计算加 2 
                    {
                        byteCount += 2;
                    }
                    else
                    // 按英文字符计算加 1 
                    {
                        byteCount += 1;
                    }
                    if (byteCount > len)
                    // 超出时只记下上一个有效位置
                    {
                        pos = i;
                        break;
                    }
                    else if (byteCount == len)// 记下当前位置
                    {
                        pos = i + 1; break;
                    }
                } if (pos >= 0)
                {
                    result = str.Substring(0, pos);
                }
            }
            else { result = str; } return result;
        }
    }
}
原文地址:https://www.cnblogs.com/acoll/p/3030143.html