简体   繁体   English

Android ArrayList过滤器特殊字符

[英]Android ArrayList filter special characters

In my app, I have an ArrayList with all my Contacts of my Phone Book. 在我的应用程序中,我有一个ArrayList,其中包含我的电话簿中的所有联系人。

Unfortunately there are records like "tom@test.com". 不幸的是,有诸如“ tom@test.com”之类的记录。 How can i remove all the records with special characters, so that i only get letters-only ones? 我如何删除所有带有特殊字符的记录,以便只得到字母的记录?

        Cursor cur = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI,
            new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null,
            null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
    int i = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
        contacts.add(cur.getString(i));
    }

You can use PhoneNumberUtils.stripSeparators(String) (available in API 1+). 您可以使用PhoneNumberUtils.stripSeparators(String)(在API 1+中可用)。

Or you can use regular expression‌​: 或者您可以使用正则表达式‌:

filterNum = filterNum.replaceAll("[^0-9]+", "");

which will remove all characters which are not in range 0...9. 这将删除所有不在0 ... 9范围内的字符。 I think it would be easier. 我认为这样会更容易。 Here's the documentation. 这是文档。

what is contacts?? 什么是联系人? I think u must replace every ur arraylist with replaceAll old char to new char 我认为您必须使用replaceAll将所有旧字符替换为新字符

for (Contact contact : contacts)
   contact.getText.replaceAll("@"," ");

I think it should be something like this (if this is not the solution for the problem write me :)): 我认为应该是这样的(如果这不是解决问题的方法,请写信给我:)):

boolean specialChar;
    Cursor cur = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI,
            new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null,
            null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
    int i = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
        //          contacts.add(cur.getString(i));
        specialChar = false;
        for(char c : cur.getString(i).toCharArray()){
            specialChar = false;
            if(!Character.isDigit(c) || !Character.isLetterOrDigit(c)){
                specialChar = true;
            }
            if(!specialChar){
                contacts.add(cur.getString(i));
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM