我对该部分有疑问。如何替换字符串之间的空格,何时从联系人列表中获取电话号码? 并且它工作正常,但有些Android设备(例如三星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 :(得分:3)
String
类有一个方法:
.replace(char oldChar, char newChar)
它会返回一个新的String
,这是因为使用oldChar
替换此字符串中所有出现的newChar
。所以你只是用空String
替换所有空格(即""
):
phoneNo = cursor.getString(phoneIndex);
String phoneNumber= str.replaceAll(" ", ""); // your string without any spaces
答案 1 :(得分:1)
只需更换电话号码中的每个空格
即可phoneNo=phoneNo.replaceAll("\\s+","");