Home » Android » java – How to get phone contact without country code?

java – How to get phone contact without country code?

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.

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

Answer:

Use this as your code…

 import java.util.ArrayList;

public class aaaaa {

    public static void main(String[] args) {
        ArrayList<String> al = new ArrayList<String>();
        al.add("+91");
        String input = "+91-9876543210";
        if (input.indexOf("+") > -1) {
            String str = input.split("-")[0];
            if (al.contains(str)) {
                System.out.println(input.split("-")[1]);
            }
        }
    }
}

Add all the country codes that exist/you need to use into the ArrayList al… Or use the code as it is if you just need Indian code…

Tested with all possible country codes.

If you don’t care about the country codes and just bothered about the data between +xxx-<number>, then you can use this….

String str="+91-9876543210";;  
System.out.println(str.substring(str.indexOf('-')+1,str.length()));

Answer:

import javax.validation.constraints.NotEmpty;

import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber

public class MobileNumberUtil {
    public static String toMobileNoFromInternationPhone(@NotEmpty String 
    internationalMobileNumber) {
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        String nationalNumber = null;
        try {
            Phonenumber.PhoneNumber numberProto = 
            phoneUtil.parse(internationalMobileNumber, "");

            nationalNumber = Long.toString(numberProto.getNationalNumber());
        } catch (NumberParseException e) {
            return StringUtils.EMPTY;
       }
       return nationalNumber;
   }
}

Answer:

The following code can be used to do this.

public static void main(String[] args) {
    String input="+91-9876543210";
    if(input.indexOf("-")>-1)
    System.out.println(input.split("-")[1]);
}

Answer:

Its Very Easy.

String s="+91-9876543210";;  
System.out.println(s.substring(s.indexOf('-')+1,s.length()));

Answer:

public static String removeCountryCode(String strMobWithCD){

    String strNewMob = null;

    if(strMobWithCD.contains("+91")){

        strNewMob = strMobWithCD.replace("+91","").toString();
    }else if(strMobWithCD.contains("91")){

        strNewMob = strMobWithCD.replace("91","").toString();
    }else {

        strNewMob = strMobWithCD;
    }

    return strNewMob;
}