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         }

 

posted @ 2013-09-10 13:52  Lyghost  阅读(582)  评论(0编辑  收藏  举报