C#: Truncate a String at the End of a Word


It seems that just about everyone in the world has their own version of this method so I thought I would share mine as well. 

public static string Truncate(this string s, int length, bool atWord, bool addEllipsis)
{
     // Return if the string is less than or equal to the truncation length
     if (s == null || s.Length <= length)
          return s;

     // Do a simple tuncation at the desired length
     string s2 = s.Substring(0, length);

     // Truncate the string at the word
     if (atWord)
     {
          // List of characters that denote the start or a new word (add to or remove more as necessary)
          List<char> alternativeCutOffs = new List<char>() { ' ', ',', '.', '?', '/', ':', ';', '\'', '\"', '\'', '-' };

          // Get the index of the last space in the truncated string
          int lastSpace = s2.LastIndexOf(' ');

          // If the last space index isn't -1 and also the next character in the original
          // string isn't contained in the alternativeCutOffs List (which means the previous
          // truncation actually truncated at the end of a word),then shorten string to the last space
          if (lastSpace != -1 && (s.Length >= length + 1 && !alternativeCutOffs.Contains(s.ToCharArray()[length])))
               s2 = s2.Remove(lastSpace);
     }

     // Add Ellipsis if desired
     if (addEllipsis)
          s2 += "...";

     return s2;
}

This method returns a truncated string cut off at the nearest “end of word” location (the returned string will always be less than or equal to the length parameter).  The twist that I put on my version is that you can specify alternative cut offs rather that just spaces.  To illustrate the benefit of this, see the code below.

string s = "I like green, red, and yellow!";
string s2 = s.Truncate(12, true, false);

A straight forward truncation at the nearest space would result in the string “I like” as the last space before character 12 occurs right after the word “like”. But, ideally we want to truncate after green as that is the end of a word. In the code above, we check the next character after the ‘n’ in green and realize it is a comma, an alternative cut off, and preserve the word “green”.

For all you die-hard regex guys out there I do realize this can be done with less lines of code using regex, but this example doesn’t!

This method also takes advantage of the extension methods provided by the .Net Framework 3.5, if you didn’t notice already.

Follow

Get every new post delivered to your Inbox.

Join 68 other followers