如何删除国家代码,当从联系人中挑选电话号码时

如何删除国家代码,当从联系人中挑选电话号码时

问题描述:

enter image description here如何删除国家代码,当从联系人中挑选电话号码时

我对该部分有疑问。当我从联系人列表中选择电话号码时如何删除国家/地区代码?

例如:+91 999999999而不是9999999999或+020 9696854549而不是9696854549任何人都可以知道关于我的问题的答案。请给这个问题的解决办法

我附上我的代码和图像在这里。

 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(); 
} 
} 
+0

@IntelliJAmiya这将仅适用于有三个国家代码的国家工作数字。美国有1,很多国家有2 ... –

+1

没有什么说所有的电话号码必须有一个国家代码(在联系人列表中)。但是,您可以通过查找“+”来检查它,然后删除它后面的数字(直到第一个空格)。 –

+0

@PeterAbolins确实 –

使用此功能希望这会帮助你:

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

如果你得到你的解决方案,请upvote我的答案 –

我的英语很差,但我会尽我所能来回答你的问题。

首先,这一行添加到您的build.gradle

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

而且下面是科特林

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 
} 

样本写。如果电话号码不与“+”启动然后输入国家代码,然后输入地区信息,例如:

VAL phoneNumber的= phoneInstance.parse(电话, “CN”)

您可以使用startsWith()

这个方法有两种变体和测试一个字符串是否以 指定的前缀开头的开始指定索引或默认在 开头。

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); 
} 
+1

谢谢。但你的解决方案给予删除前4位数字。我从你的答案中得到解决方案例如:+917854129856它会给54129856.当我问的时候,这不是完美的解决方案。我需要得到7854129845 – New

+2

非常感谢。它的工作正常。 – New

+1

@venkateshwari很高兴帮助。前进 。 –

它将为所有实例工作。你不必关心国家代码有多少位是

String get_Mo = phoneNumber.substring(phoneNumber.lastIndexOf(' ')+1));