使用startsWith和replaceFirst不适合我。

时间:2017-04-07 08:21:55

标签: android startswith

我使用下面的代码打印Android监视器中的所有手机通讯录。如果电话号码以00开头,我想将00更改为+。但它没有用。你能告诉我有什么问题吗?

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);

        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(
                        ContactsContract.Contacts.DISPLAY_NAME));

                if (cur.getInt(cur.getColumnIndex(
                        ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                            new String[]{id}, null);
                    while (pCur.moveToNext()) {
                        String phoneNo = pCur.getString(pCur.getColumnIndex(
                                ContactsContract.CommonDataKinds.Phone.NUMBER));

                        if (phoneNo.startsWith("00")) {
                            System.out.println(phoneNo.replaceFirst("00", "+"));
                        }

                        System.out.println("Name: " + name);
                        System.out.println("Phone No: " + phoneNo);

                    }
                    pCur.close();
                }
            }
        }

1 个答案:

答案 0 :(得分:3)

replaceFirst()返回phoneNo = phoneNo.replaceFirst("00", "+") ,它不会改变对象。你应该执行任务:

!important