Getting First N words from strings or a title in C#
ASP.NET September 1, 2012 Comments Off on Getting First N words from strings or a title in C#We can use regular expression to get the first N words from a title or strings. The following example shows how it can be done.
This example utilizes RegularExpression namespace
eg
using System.Text.RegularExpressions; string testwords = "I want only first N words from this strings"; string firstnWords = Regex.Match( testwords , @"^(\w+\b.*?){3}").ToString(); label_Result.Text = firstnWords;
Result
I want only