0

I have created a news section for a website I'm working on. I'd like to show the first few words of the article then a link to the rest. I'm kicking myself because I remember a few months ago seeing an article on exactly how to do this, but I have no idea where I found it.

I know how to grab the first so many characters from a string but it gets more tracking when you are trying to grab words instead. If anyone could point me in the direction of a tutorial or article along these things I’d be very grateful.

flyersun
  • 917
  • 3
  • 15
  • 35

2 Answers2

2

I'd probably do this with regex. See example below:

private string FindFirstWords (string input, int howManyToFind)
{
      string REGEX = @"([\w]+\s+){" + howManyToFind + "}";
      return Regex.Match(input,REGEX).Value;
}

Stolen from http://weblogs.asp.net/rosherove/archive/2005/01/07/348138.aspx

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
2

Here is a link to a blog that will return the first X words from a string.

http://dotnetperls.com/first-words

(Please note I haven't written or tested this code)

Nathan Koop
  • 24,803
  • 25
  • 90
  • 125