Remove all Lower Case Letters using C#

I came across a discussion where the OP wanted to use only the first letter of a word in a string. The first letter of each word was capitalized, so his requirement was to abbreviate the sentence by removing all lower case letters in a string.

For example: ‘We Love’ would become ‘WLDC’

Here’s how this can be achieved with a Regular expression. Use the following code:

using System;
using System.Text.RegularExpressions;

namespace WordCapital
class Program
static void Main(string[] args)
string fulltext = "We Love";
Console.WriteLine("Full Text: {0}", fulltext);
Regex rgx = new Regex("[^A-Z]");
Console.WriteLine("Abbreviated Text : {0}", 
rgx.Replace(fulltext, ""));

In the code above, we are using Regex to match all characters that are not capitalized and then replace it with an empty string.



About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: