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.


4 Responses to “C#: Truncate a String at the End of a Word”

  1. Gerard Says:

    Thanks for sharing.
    However, you probably forgot the generic type variable in the declaration of the alternativeCutOffs…

    • Nick Olsen Says:

      A true statement! Thanks for catching that. I have fixed the post.

  2. John Says:


    Just wanted to say thanks. It was the least I could do as I’ve just lifted your code.

    I’ve been testing string.replace vs regex.replace vs stringbuilder to get the first 20-30 words of a string (x5), but found it added about 1.5 seconds to my page load.

    Your code keeps it down to milliseconds 🙂

    Thanks again.


    • Nick Olsen Says:

      Fantastic! I’m glad I helped.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: