c#完美截断字符串(中文+非中文)
生活随笔
收集整理的這篇文章主要介紹了
c#完美截断字符串(中文+非中文)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public static string Truncation(this HtmlHelper htmlHelper, string str, int len){if (str == null || str.Length == 0 || len <= 0){return string.Empty;}int l = str.Length;#region 計算長度int clen = 0;while (clen < len && clen < l){//每遇到一個中文,則將目標長度減一。if ((int)str[clen] > 128) { len--; }clen++;}#endregionif (clen < l){return str.Substring(0, clen) + "...";}else{return str;}}
轉載于:https://www.cnblogs.com/ptfblog/archive/2012/06/07/2540143.html
總結
以上是生活随笔為你收集整理的c#完美截断字符串(中文+非中文)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jieba分词原理
- 下一篇: C#操作XML之——读取XML文件