Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get phone contact without country code?

Tags:

java

android

I can get contact number from phone. I want to know how to remove the country code from it. That is if user has the phone number with country code(+91-9876543210), i need the phone number without prefixed country code(9876543210).

Thanks in advance.

like image 504
khaleel_jageer Avatar asked Jul 02 '15 04:07

khaleel_jageer


1 Answers

I'd suggest to use libphonenumber to parse the phone numbers easily. You can split the country code and phone number in this way.

try {
    // phone must begin with '+'
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    Phonenumber.PhoneNumber numberProto = phoneUtil.parse("+91-9876543210", "");
    int countryCode = numberProto.getCountryCode();
    long nationalNumber = numberProto.getNationalNumber();
    Log.i("code", "code " + countryCode);
    Log.i("code", "national number " + nationalNumber);
} catch (NumberParseException e) {
    System.err.println("NumberParseException was thrown: " + e.toString());
}
like image 146
Ye Lin Aung Avatar answered Oct 22 '22 06:10

Ye Lin Aung