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.



Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: