1 /// <summary>
2 /// 将指定的字符串按指定的长度剪切
3 /// </summary>
4 /// <param name="oldStr">需要剪切的字符串</param>
5 /// <param name="maxLength">需要字符串的最大的长度</param>
6 /// <param name="endWith">超过长度的后缀</param>
7 /// <returns>如果超过长度,返回截取的字符串加上后缀;否则返回原字符串</returns>
8 public static string StringTruncate(string oldStr, int maxLength, string endWith)
9 {
10 if (string.IsNullOrEmpty(oldStr))
11 return oldStr + endWith;
12 if (maxLength < 1)
13 throw new Exception("返回的字符串长度必须大于[0]");
14 if (oldStr.Length > maxLength)
15 {
16 string strTmp = oldStr.Substring(0, maxLength);
17 if (string.IsNullOrEmpty(endWith))
18 return oldStr;
19 else
20 return strTmp + endWith;
21 }
22 return oldStr;
23 }
24 }