RSS Feed for This PostCurrent Article

Google’s library for dealing with phone numbers

libphonenumber is a Google’s common Java library for parsing, formatting, storing and validating international phone numbers. Optimized for running on smartphones. A direct port to Javascript is also available.

Let’s say you have a string representing a phone number from Switzerland, this is how you parse/normalize it into a PhoneNumber object:

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());
}

At this point, swissNumberProto contains:

{
country_code: 41
national_number: 446681800
}


Trackback URL


Sorry, comments for this entry are closed at this time.