关键代码如下:

/// <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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
  • 2021-08-03
  • 2022-12-23
猜你喜欢
  • 2022-01-05
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2022-02-19
  • 2022-12-23
  • 2021-12-27
相关资源
相似解决方案