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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With