Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Phone Numbers to their parts

I know about the C# port of the Google libphonenumber Parsing Library: http://code.google.com/p/libphonenumber/

What I need is to take a phone number string and break it up into the corresponding pieces, Country Code, Area Code, Prefix, Number, and Extension.

Can this library be used to do that? If so, can someone post a simple test in C# to do that? I don't see how to do it in the docs.

BTW, they can be domestic or international.

like image 259
MB34 Avatar asked Sep 22 '12 02:09

MB34


People also ask

How is a phone number divided?

In the U.S. and Canada, the parts of a phone number are the exit code, country code, area code, telephone prefix, and line number. The exit code lets you dial out of your home country. The country code is an identifier for a specific country. The area code directs calls to a broad region.

How do you format a phone number?

To format phone numbers in the US, Canada, and other NANP (North American Numbering Plan) countries, enclose the area code in parentheses followed by a nonbreaking space, and then hyphenate the three-digit exchange code with the four-digit number.

What is the phone number sequence?

Area code and other parts of a phone number Phone numbers in the United States typically consist of 11 digits — the 1-digit country code, a 3-digit area code and a 7-digit telephone number. The 7-digit telephone number is further comprised of a 3-digit central office or exchange code and a 4-digit subscriber number.


2 Answers

The libphonenumber library will parse a number and validate that it matches a known pattern for domestic and international numbers. It will tell you the country code and the correct dialing pattern domestically or internationally for any given number.

It will not parse it into constituent parts beyond that. No area code, prefix, number, extension parsing.

It's open source so if you need to do this it might be a good starting place, but I'm sure it'll be a huge undertaking.

like image 123
Samuel Neff Avatar answered Sep 29 '22 05:09

Samuel Neff


Patrick Mezard has kindly ported the library to C#:

https://bitbucket.org/pmezard/libphonenumber-csharp/wiki/Home

For usage, you can look at the official web site:

http://code.google.com/p/libphonenumber/

The Java code can be directly translated to C#. For example:

Java

String swissNumberStr = "044 668 18 00"
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
  PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "CH");
} catch (NumberParseException e) {
  System.err.println("NumberParseException was thrown: " + e.toString());
}

C#

String swissNumberStr = "044 668 18 00";
PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();
try
{
    PhoneNumber swissNumberProto = phoneUtil.Parse(swissNumberStr, "CH");
    Console.WriteLine(swissNumberProto.CountryCode);
}
catch (NumberParseException e)
{
    Console.WriteLine("NumberParseException was thrown: " + e.ToString());
}

Good luck.

Update:

More examples: http://code.google.com/p/libphonenumber/source/browse/#svn/trunk/java/libphonenumber/test/com/google/i18n/phonenumbers

If you don't see what you need, then I guess you can implement it yourself.

like image 38
Cam Avatar answered Sep 29 '22 03:09

Cam