·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 计算中英文混合字符串的宽度

计算中英文混合字符串的宽度

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

计算中英文混合字符串的宽度

关键代码如下:

/// <summary>/// 估算中英文字符串的宽度/// </summary>/// <returns></returns>public static double GetWidthUnitCount(string value){    double count = 0;    for (var i = 0; i < value.Length; i++)    {        if (IsChinese(value[i].ToString()) == true)        {            count += 2;        }        else if (IsUpChar(value[i].ToString()) == true)        {            count += 1.5;        }        else        {            count += 1;        }    }    return count;}/// <summary>/// 是否汉字或中文标点/// </summary>PRivate static bool IsChinese(string value){    Regex reg = new Regex("[\u4E00-\u9FFF]|[\uFE30-\uFFA0]");    return reg.IsMatch(value);}/// <summary>/// 是否大写字母/// </summary>private static bool IsUpChar(string value){    Regex reg = new Regex("[A-Z]");    return reg.IsMatch(value);}
View Code