Just thought I’d write a quick post with some extension methods that you can use for cleaning up strings.

The core of these extension methods are regular expressions, and if you understand how they work, you can roll your own using this pattern.
If you don’t, you should read these:
http://msdn.microsoft.com/en-us/library/hs600312.aspx
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

I feel that mastering Regular Expressions is very important for string manipulation.

Let’s take the example of an application which provides a text-box for phone numbers, where the user can enter a phone-number in any format and then you would just like to extract the numbers to do a search.
It boils down to stripping the input of non-numeric characters.

I would write an extension method like this:


namespace String.Util
{
    public static class Extensions
    {

        public static string StripNonNumbers(this string input)
       {
             //strip out non-numeric chars from input
            string stripped = Regex.Replace(input, "[^0-9]", "");
            return stripped;
      }

.
.
.

The Regular Expression above essentially matches non-numeric characters and replaces them with an empty string, so you get a resulting string containing only numbers.

You could tweak this with some really powerful expressions for your purpose.
For e.g. In place of [^0-9], use [^a-zA-Z] to remove non-alpha characters or [0-9] to replace numeric characters

Then you just reference String.Util in your target module with a Using statement and invoke it like:


string cleanPhone = phone.StripNonNumbers();

Notice the down-arrow in the Intellisense for a string’s methods, which indicates that it’s an extension method.

If you’re not already familiar with Extension methods, they are a cleaner, functional programming way of writing Helper methods. They are not more efficient necessarily but make the code much cleaner and I would encourage you to use them when possible.

Advertisements