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 DevCurry.com’ 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 DevCurry.com";
Console.WriteLine("Full Text: {0}", fulltext);
Regex rgx = new Regex("[^A-Z]");
Console.WriteLine("Abbreviated Text : {0}", 
rgx.Replace(fulltext, ""));
Console.ReadLine();
}
}
}

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

OUTPUT

image

No comments:

Post a Comment