0

I have to read contacts from device which are having valid phone numbers. For that right now I'm using following query.

        final Uri contentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        final String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '" + ("1") + "' AND  LENGTH(" + ContactsContract.CommonDataKinds.Phone.NUMBER + ") >= 10";
        final String[] projection = null;
        final String[] selectionArgs = null;
        final String sortOrder = "upper(" + ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID + ") ASC";
        Cursor phones = mContentResolver.query(contentUri, projection, selection, selectionArgs, sortOrder);

As you can see in selection , im querying for entries with HAS_PHONE_NUMBER = 1 and length of phone number >= 10 , to avoid maximum junk contacts while retrieving from itself. It works fine , but the problem is , the saved phone numbers might have special characters like - , ( , )or a space etc. Like +9191-91-22-22-55 , +9191-91-(22 22-55) . I need to get this string without non-digits and without country code (last 10 numbers).

For example : +9190-91-22-22-55 as 9091222255

is there any way to retrieve a string field as formatted like this in SQLite? Or is there any effective way to fulfill my requirement? Thanks in advance..

  • a simple `String::replaceAll`, which takes a regex: `value.replaceAll("[^0-9]", "");` should do. – njzk2 Jul 09 '16 at 15:44
  • Can it be used in query itself? `value.replaceAll("[^0-9]", "");` is in java code right? so that I need to iterate each record again and do this? –  Jul 09 '16 at 15:55
  • yes, do that when you read the values – njzk2 Jul 09 '16 at 15:57
  • you are telling use this in java or sqlite query? –  Jul 09 '16 at 16:02
  • can you pls show me a sample query with replace? –  Jul 09 '16 at 16:26
  • Your assumptions about phone numbers (everything before the last 10 digits is the country code) is wrong almost everywhere. – CL. Jul 09 '16 at 18:23
  • May be , but in my requirement I just need the last 10 digits , it could be valid phone number or not , but I am just using this 10 digit number as some ID . –  Jul 10 '16 at 08:13

0 Answers0