How to remove words based on a word count

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-05-24T19:13:10Z Indexed on 2010/05/24 19:31 UTC
Read the original article Hit count: 182

Filed under:
|
|

Here is what I'm trying to accomplish. I have an object coming back from the database with a string description. This description can be up to 1000 characters long, but we only want to display a short view of this. So I coded up the following, but I'm having trouble in actually removing the number of words after the regular expression finds the total count of words. Does anyone have good way of dispalying the words which are less than the Regex.Matches?

Thanks!

if (!string.IsNullOrEmpty(myObject.Description))
{
    string original = myObject.Description;
    MatchCollection wordColl = Regex.Matches(original, @"[\S]+");
    if (wordColl.Count < 70) // 70 words?
    {
        uxDescriptionDisplay.Text = 
             string.Format("<p>{0}</p>", myObject.Description);
    }
    else
    {                        
        string shortendText = original.Remove(200); // 200 characters?
        uxDescriptionDisplay.Text = 
              string.Format("<p>{0}</p>", shortendText);
    }
 }

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex