...
/// <summary> /// 將指定的字元串按指定的長度剪切 /// </summary> /// <param name="oldStr">需要剪切的字元串</param> /// <param name="maxLength">需要字元串的最大的長度</param> /// <param name="endWith">超過長度的尾碼</param> /// <returns>如果超過長度,返回截取的字元串加上尾碼;否則返回原字元串</returns> public static string StringTruncate(string oldStr, int maxLength, string endWith) { if (string.IsNullOrEmpty(oldStr)) return oldStr + endWith; if (maxLength < 1) throw new Exception("返回的字元串長度必須大於[0]"); if (oldStr.Length > maxLength) { string strTmp = oldStr.Substring(0, maxLength); if (string.IsNullOrEmpty(endWith)) return oldStr; else return strTmp + endWith; } return oldStr; } }