2017-08-01 94 views
2

我对该部分有疑问。如何更换字符串之间的空格,何时从联系人列表中获取电话号码? 它的工作正常,但一些Android设备(例如Samsung Tab)在其联系人中添加了 空间。我从联系人得到的号码。所以我得到字符串99 99 99999而不是99999999.从联系人中挑选电话号码

而且,如何从该数字中消除国家代码。 例如:+91 999999999而不是9999999999或+020 9696854549而不是9696854549

我知道,通过使用.replace()删除该空间。
是否有任何其他进程删除字符串之间的空间。

我重视我的代码:::

public void onClick(View view) { 
      Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, 
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI); 
       startActivityForResult(contactPickerIntent, 
RESULT_PICK_CONTACT); 
     } 

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); 

     mobile_et.setText(phoneNo); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

也许你可以使用https://github.com/googlei18n/libphonenumber – ZeekHuge

+0

https://github.com/googlei18n/libphonenumber – YUVRAJ

回答

0

只需更换每一个空白的电话号码

phoneNo=phoneNo.replaceAll("\\s+",""); 
2

String类有一个方法:

.replace(char oldChar, char newChar) 

它返回由于替换所有出现的oldChar而产生的新String在这个字符串中与newChar。所以,你只是一个空String(即"")替换所有空格:

phoneNo = cursor.getString(phoneIndex); 
String phoneNumber= str.replaceAll(" ", ""); // your string without any spaces 
相关问题