public static string TrimMiddle(this string @this, int limitLength, string omitPlaceholder = null) { const string defaultOmitPlaceholder = "..."; if (omitPlaceholder == null) { omitPlaceholder = defaultOmitPlaceholder; } Guards.ThrowIfNot(limitLength >= omitPlaceholder.Length); if (string.IsNullOrEmpty(@this) || @this.Length <= limitLength) { return(@this); } if (limitLength == omitPlaceholder.Length) { return(omitPlaceholder); } var halfLength = (limitLength - omitPlaceholder.Length) / 2; var bias = (limitLength - omitPlaceholder.Length) % 2; var firstHalfString = @this.Substring(0, halfLength + bias); var secondHalfString = @this.Substring(@this.Length - halfLength); return($"{firstHalfString}{omitPlaceholder}{secondHalfString}"); }