I have created a news section for a website I'm work开发者_高级运维ing 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.
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)
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
精彩评论