C#中英文混合字符串过长截断
1 /// <summary> 2 /// 截断字符串 3 /// </summary> 4 /// <param name="maxLength">最大长度</param> 5 /// <param name="str">原字符串</param> 6 /// <returns></returns> 7 public static string CutStr(int maxLength, string str) 8 { 9 string temp = str; 10 if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= maxLength) 11 { 12 return temp; 13 } 14 for (int i = temp.Length; i >= 0; i--) 15 { 16 temp = temp.Substring(0, i); 17 if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= maxLength - 3) 18 { 19 return temp + "..."; 20 } 21 } 22 return "..."; 23 }