Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract code country from phone number [libphonenumber]

I have a string like this : +33123456789 (french phone number). I want to extract the country code (+33) without knowing the country. For example, it should work if i have another phone from another country. I use the google library https://code.google.com/p/libphonenumber/.

If I know the country, it is cool I can find the country code :

PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); int countryCode = phoneUtil.getCountryCodeForRegion(locale.getCountry()); 

but I don't find a way to parse a string without to know the country.

like image 833
mrroboaat Avatar asked May 29 '13 08:05

mrroboaat


People also ask

What is a Libphonenumber Android?

GitHub - google/libphonenumber: Google's common Java, C++ and JavaScript library for parsing, formatting, and validating international phone numbers. Product.

How does Google Libphonenumber work?

Google's libphonenumber is a library that parses, formats, stores and validates international phone numbers. It is used by Android since version 4.0 and is a phenomenal repository of carrier metadata. Although it compiles down to Java, C++ and JS, its JS port is tightly coupled to the Google Closure library.


1 Answers

Okay, so I've joined the google group of libphonenumber ( https://groups.google.com/forum/?hl=en&fromgroups#!forum/libphonenumber-discuss ) and I've asked a question.

I don't need to set the country in parameter if my phone number begins with "+". Here is an example :

PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); try {     // phone must begin with '+'     PhoneNumber numberProto = phoneUtil.parse(phone, "");     int countryCode = numberProto.getCountryCode(); } catch (NumberParseException e) {     System.err.println("NumberParseException was thrown: " + e.toString()); } 
like image 159
mrroboaat Avatar answered Oct 02 '22 03:10

mrroboaat