protected TruncatedString(string str, uint maxLength, string optionalEllipsis = "...") : base(ss => ss) { this.massager = this.Massage; this.MaxLen = (int)maxLength; this.Ellipsis = optionalEllipsis; // ReSharper disable once DoNotCallOverridableMethodsInConstructor this.String = str; }
public static string TruncateIfExceeds(this string str, uint maxLen, string optionalEllipsis = "...") { if (str == null || str.Length <= maxLen) { return(str); } NonNullString ellipsis = optionalEllipsis; if (ellipsis.String.Length > maxLen) { ellipsis = (string)null; } // ReSharper disable once PossibleNullReferenceException string truncated = str.Substring(0, (int)maxLen - ellipsis.String.Length); truncated = string.Format("{0}{1}", truncated, ellipsis); return(truncated); }