public static string Truncate(this string text, int maxLength, TextTruncateType textTruncateType, string trailerText)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(string.Empty); //? throw new ArgumentNullException("text");
            }
            if (maxLength < 0)
            {
                throw new ArgumentOutOfRangeException("maxLength");
            }
            if (string.IsNullOrEmpty(trailerText))
            {
                throw new ArgumentNullException("trailerText");
            }
            var trailerTextLength = trailerText.Length;

            switch (textTruncateType)
            {
            case TextTruncateType.Normal:
                return(text.Length > maxLength ? (maxLength > trailerTextLength ? text.Substring(0, maxLength - trailerTextLength) + trailerText : text.Substring(0, maxLength)) : text);

            case TextTruncateType.LastWhitespace:
                if (text.Length > maxLength)
                {
                    if (maxLength > trailerTextLength)
                    {
                        var    truncatedText = text.Substring(0, maxLength - trailerTextLength);
                        var    whiteIndex    = truncatedText.LastIndexOf(' ');
                        string truncatedTextUntilWhite;
                        return(((whiteIndex > 0) && ((truncatedTextUntilWhite = truncatedText.Substring(0, whiteIndex).Trim()).Length > 0) ? truncatedTextUntilWhite : truncatedText) + trailerText);
                    }
                    return(text.Substring(0, maxLength));
                }
                return(text);

            case TextTruncateType.FromAtomSite:
                // Add room for a word not breaking and the trailer
                var b     = new StringBuilder(maxLength + 20);
                var words = text.Split(new char[] { ' ' });
                var index = 0;
                while (b.Length + words[index].Length + trailerTextLength < maxLength - trailerTextLength && index < words.GetUpperBound(0))
                {
                    b.Append(words[index]);
                    b.Append(" ");
                    index++;
                }
                // We exited the loop before reaching the end of the array - which would normally be the case.
                if (index < words.GetUpperBound(0))
                {
                    // Remove the ending space before attaching the trailer.
                    b.Remove(b.Length - 1, 1);
                    b.Append(trailerText);
                }
                else
                {
                    b.Append(words[index]);
                }
                return(b.ToString());

            default:
                throw new InvalidOperationException();
            }
        }
 /// <summary>
 /// Truncates the specified text.
 /// </summary>
 /// <param name="text">The text.</param>
 /// <param name="maxLength">Length of the max.</param>
 /// <param name="textTruncateType">Type of the text truncate.</param>
 /// <returns></returns>
 public static string Truncate(this string text, int maxLength, TextTruncateType textTruncateType)
 {
     return(Truncate(text, maxLength, textTruncateType, "..."));
 }