Monday, June 15, 2009

IFormatProvider for North American Phone Numbers

CodeProject - North American Phone Number Format Provider (iFormatProvider implementation)

“…

What problem does this solution solve

Re-formatting and normalizing irregularly entered/stored North American phone numbers

How does this help

Having uniformed phone numbers will allow programmer/system administrator or any other IT professional to store clean data into the receiving data-source

Using the code

Here in North America we are lucky to have uniformed phone numbering plan (from programmer stand-point) , known as North American Numbering Plan (NANP); NANP makes parsing of the phone number relatively easy; This article covers only North American phone number format and does not attempt to parse any other formats for any other phone systems. Direct application of this custom format provider to other types of phone numbers could result in unpredictable results. However you can extend this code to process other types of the phone numbers by adding methods that would recognize formats of the phone numbers specific to your local phone system. Good example would be French phone system, which is persistent in its numbering rules and therefore can be quantified by format provider relatively easy.

How does the code actually work

The implementation of IFormatProvider interface is rather well documented on MSDN site. This particular application of the IFormatProvider works with several predefined "codes" to distinguish between several desirable outcome formats of the string.

Understood formats:

{0:a} Example: 1-555-563-3434 (Hyphenated)
{0:c} Example: 15555633434 (Numeric only)
{0:d} Example: +1 (555) 123-4567 (Default)
{0:de} Example: 1 (555) 563-3434 ex 5555 (Default with Extension)
{0:e} Example: 1-555-563-3434 ex 5555 (Extension)
{0:s} Example: 1 555 563 3434 (Space)

…”

I’ve not done with custom IFormatProvider implementations, so when I saw this I thought it was kind of cool… It’s not a ton of code nor level 400+ but I still thought it was cool. Why? Because I liked how it captures a solution to a common problem and that the author took the time to not only code it up but also share his time saver with the rest of us… :)

1 comment:

  1. To use IFormatProvider you have to pass the culture info for DateTime format coz in different cultures DateTime format of displaying the sequence of month and date varies.

    ReplyDelete

NOTE: Anonymous Commenting has been turned off for a while... The comment spammers are just killing me...

ALL comments are moderated. I will review every comment before it will appear on the blog.

Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...

I reserve, and will use, the right to not approve ANY comment for ANY reason. I will not usually, but if it's off topic, spam (or even close to spam-like), inflammatory, mean, etc, etc, well... then...

Please see my comment policy for more information if you are interested.

Thanks,
Greg

PS. I am proactively moderating comments. Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...