Select a phone number from contacts

I have doubts about this section. How to replace the space between the line when you need to get a phone number from your contacts list? And it works great, but some Android devices (like Samsung Tab) have it added to their contacts. I receive a number from a contact. So I get the string 99 99 99999 instead of 99999999.

And also how to remove the country code from this number. Example: +91 999999999 instead of 9999999999 or +020 9696854549 instead of 9696854549

I know remove this space using .replace ().
 Is there any other way to remove the space between the line.

I have attached my code:

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

      

+3


source to share


2 answers


Just replace every free space with a phone number



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

      

+1


source


The class String

has a method:

.replace(char oldChar, char newChar)

      



It returns a new String

one resulting from replacing all occurrences oldChar

in this string newChar

. This way you just replace all spaces with blank ones String

(i.e. ""

):

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

      

+3


source







All Articles