Phil Fresle's Developer Blog

Extract the first N words from a string with C#

This code will return the first 5 words, change the number in the regular expression as needed:

string testString = "The quick brown fox jumps over the lazy dog."
string firstWords = Regex.Match(testString, @"^(\w+\b.*?){5}").ToString();



Feedback

# re: Extract the first N words from a string with C#

work like a charm 4/24/2012 3:03 PM | Aziz ur Rahman