How to remove country code , When Pick Phone Number from contacts

20,667

Solution 1

use this function hope it will help you out:

public String phoeNumberWithOutCountryCode(String phoneNumberWithCountryCode) {
        Pattern complie = Pattern.compile(" ");
        String[] phonenUmber = complie.split(phoneNumberWithCountryCode);
        Log.e("number is", phonenUmber[1]);
        return phonenUmber[1];
    }

Solution 2

My English is poor, but I will try my best to answer your question.

First of all , add this line to your build.gradle

compile 'com.googlecode.libphonenumber:libphonenumber:8.7.0'

And below is a sample write by kotlin

fun deleteCountry(phone: String) : String{
    val phoneInstance = PhoneNumberUtil.getInstance()
    try {
        val phoneNumber = phoneInstance.parse(phone, null)
        return phoneNumber?.nationalNumber?.toString()?:phone
    }catch (_ : Exception) {
    }
    return phone
}

If phone number not start with a '+' followed by the country calling code then you should pass region information, for example:

val phoneNumber = phoneInstance.parse(phone, "CN")

Solution 3

You can use startsWith()

This method has two variants and tests if a string starts with the specified prefix beginning a specified index or by default at the beginning.

if(phoneNumber.startsWith("+"))
{
    if(phoneNumber.length()==13)
    {
    String str_getMOBILE=phoneNumber.substring(3);
    mobile_et.setText(str_getMOBILE);
    }
    else if(phoneNumber.length()==14)
    {
    String str_getMOBILE=phoneNumber.substring(4);
    mobile_et.setText(str_getMOBILE);
    }


}
else
{
 mobile_et.setText(phoneNumber);
}

Solution 4

I am just simply extending the answer that If you don't want to use any library then you can do it in this way. In order to match any prefix and since some countries could share partially the same prefix (for example 91 and 91-721), you add all possibilities to the regex in descending order and size.

Follow this Code:

public static String PhoneNumberWithoutCountryCode(String phoneNumberWithCountryCode){//+91 7698989898
        Pattern compile = Pattern.compile("\\+(?:998|996|995|994|993|992|977|976|975|974|973|972|971|970|968|967|966|965|964|963|962|961|960|886|880|856|855|853|852|850|692|691|690|689|688|687|686|685|683|682|681|680|679|678|677|676|675|674|673|672|670|599|598|597|595|593|592|591|590|509|508|507|506|505|504|503|502|501|500|423|421|420|389|387|386|385|383|382|381|380|379|378|377|376|375|374|373|372|371|370|359|358|357|356|355|354|353|352|351|350|299|298|297|291|290|269|268|267|266|265|264|263|262|261|260|258|257|256|255|254|253|252|251|250|249|248|246|245|244|243|242|241|240|239|238|237|236|235|234|233|232|231|230|229|228|227|226|225|224|223|222|221|220|218|216|213|212|211|98|95|94|93|92|91|90|86|84|82|81|66|65|64|63|62|61|60|58|57|56|55|54|53|52|51|49|48|47|46|45|44\\D?1624|44\\D?1534|44\\D?1481|44|43|41|40|39|36|34|33|32|31|30|27|20|7|1\\D?939|1\\D?876|1\\D?869|1\\D?868|1\\D?849|1\\D?829|1\\D?809|1\\D?787|1\\D?784|1\\D?767|1\\D?758|1\\D?721|1\\D?684|1\\D?671|1\\D?670|1\\D?664|1\\D?649|1\\D?473|1\\D?441|1\\D?345|1\\D?340|1\\D?284|1\\D?268|1\\D?264|1\\D?246|1\\D?242|1)\\D?");
        String number = phoneNumberWithCountryCode.replaceAll(compile.pattern(), "");
        //Log.e(tag, "number::_>" +  number);//OutPut::7698989898
        return number;
    }

Solution 5

Try this....

                if(number.length()>10)
                {
                    int startidx=number.length()-10;
                    String getnumber=number.substring(startidx,number.length());
                    etEmerNumber.setText(getnumber);
                }
                else
                {
                    etEmerNumber.setText(number);
                }
Share:
20,667
New
Author by

New

Updated on July 28, 2022

Comments

  • New
    New almost 2 years

    enter image description here

    I have doubt in that section. How to Remove country code, when I pick phone number from contact list?

    Ex: +91 999999999 instead of 9999999999 or +020 9696854549 instead of 9696854549 Can any one know the answer about my question. please give solution to this problem

    I attached my code and image here.

         private void contactPicked(Intent data) {
        Cursor cursor = null;
        try {
        String phoneNo = null ;
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();
        //Query the content uri
        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        // column index of the phone number
        int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER
      phoneNo = cursor.getString(phoneIndex);
       String phoneNumber = phoneNo.replaceAll(" ","");
       mobile_et.setText(phoneNumber);
    } catch (Exception e) {
        e.printStackTrace();
    }
    }