代码如下 | 复制代码 |
/// <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 oldStr + endWith; } return oldStr; } } |
时间: 2024-10-22 22:49:53