Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract the country code from mobile number

I have a list of mobile numbers with country code like "919638095998"number. I have referred the libphonenumber google example also but in that first we need to pass the country 2 digit code, which I don't know initially. So how can I know that the number is from which country?

I have also got one solution as manually extract the code one by one digit from mobile number and then compare with country code. But it is a long task and also it will give me bad performance. Can anyone suggest a proper solution?

like image 288
Nency Avatar asked Sep 04 '12 08:09

Nency


People also ask

How to remove country code from a phone number?

libphonenumber does that for you. All you have to do is add the + sign before your phone number and omit the country code when parsing the phone number. Show activity on this post.

How do I add a country code to a phone number?

All you have to do is add the + sign before your phone number and omit the country code when parsing the phone number. Show activity on this post. There is a list of all the phone numbers available for country codes on Wikipedia.

What is extract Phone numbers phone extractor?

Extract Phone Numbers Phone Extractor For Web Pages and Text Use this tool to find and extract Phone numbers in web pages, data files, ... What can this tool do? Use this tool to extract Phone numbers from web pages and data files.

How to get country code from phone number input in React Native?

In react native we can use React Native Phone Number Input NPM and YARN package to add country code input picker in our app. So in this tutorial we would learn about Example of React Native Phone Number Input Get Country Code.


1 Answers

libphonenumber does that for you. All you have to do is add the + sign before your phone number and omit the country code when parsing the phone number.

String numberStr = "+919638095998";
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
    Phonenumber.PhoneNumber numberProto = phoneUtil.parse(numberStr, "");

    System.out.println("Country code: " + numberProto.getCountryCode());
    //This prints "Country code: 91"
} catch (NumberParseException e) {
    System.err.println("NumberParseException was thrown: " + e.toString());
}
like image 197
João Neves Avatar answered Oct 21 '22 09:10

João Neves