Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider Follow Me On Twitter
Parsing Capital Letters

I found an intersting question on the ASP.NET forums this morning. How can you get a count of the capital letters in a string? My first thought was, 'This is a Job for Regex!'.

If you are not familiar with regular expressions, please get to know them, they are frustrating, fun and very powerful. They provide a way to define a pattern of sorts to parse content from a string, quickly. I use them for many things, such as parsing and reformating phone numbers, filtering profanity, etc. To use regular expressions in .NET you need to create a Regex object. You then need to define a regular expression to match the values or pattern you need. In this case I just need to match capital letters, which is a very simple regex, "[A-Z]". You can find an entire collection of these patterns on www.regexlib.com.

I used the term 'match' for a reason. The RegEx class has a Matches function that returns a MatchCollection or a collection of Matches to the pattern. The MatchCollection object has a Count property, which returns the number of matches in the string. This is the value needed to solve the problem asked on the forum.

Next I decided to go ahead and echo each of the matched capital letters just to make sure that is what was matched. You can choose to ignore case, whitespace and several other options by defining a list of RegexOptions in the constructor of the RegEx. The default is to match case, which is what I needed for this problem.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;

namespace GetCapCount
{
    class Program
    {
        static void Main(string[] args)
        {

            string crazyString = "This IS a CraZy LOOking sTrinG.";

            Regex rg = new Regex("[A-Z]");
            MatchCollection mc = rg.Matches(crazyString);

            Console.WriteLine("Total Capital Letters: " + mc.Count);

            foreach (Match item in mc)
            {
                Console.WriteLine(item.Value);
            }

            Console.ReadKey();

        }
    }
}

Go forth and Parse!!

Posted: Thursday, October 01, 2009 8:46 AM

by Chris Love

Comments

Scott Mitchell said:

LINQ chews through problems like this with a minimal number of characters and, some would argue, better readability than regular expressions. crazyString.ToCharArray().Count(c => char.IsUpper(c)); Also, no magic strings needed.
# October 2, 2009 3:42 PM

Chris Love said:

Come on Scott, you know Regex are always awesome, just see this example ;) http://thedailywtf.com/Articles/RegExp-From-Down-Under.aspx

# October 5, 2009 3:28 PM

Charli said:

Depends on how often this function is called, but I would recomend to avoid regexp when possible. The most nice looking solution is not always the best solution! (mostly it's the worst solution) From experience I know that RegExp is awfully >>> SLOW <<< and should be avoided when possible.. Better to build a function that loops through the string and counts if it's upper case or not! probably will take you the same amount of time to write, so why not?
# October 6, 2009 6:26 PM

Chris Love said:

Charli you can't be serious! If it were not for regular expressions SPAM filters, browsers, etc could not function as fast as they do. Regex are very fast and efficient.

# October 7, 2009 10:47 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS