关键代码如下:
/// <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); }