https://www.jb51.net/article/63086.htm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 /// <summary> /// 截取文本,区分中英文字符,中文算两个长度,英文算一个长度 /// </summary> /// <param name="str">待截取的字符串</param> /// <param name="length">需计算长度的字符串</param> /// <returns>string</returns> public static string GetSubString(string str, int length) { string temp = str; int j = 0; int k = 0; for (int i = 0; i < temp.Length; i++) { if (Regex.IsMatch(temp.Substring(i, 1), @"[\u4e00-\u9fa5]+")) { j += 2; } else { j += 1; } if (j <= length) { k += 1; } if (j > length) { return temp.Substring(0, k) + ".."; } } return temp; }