Android - How to filter emoji (emoticons) from a string?

前端 未结 4 1287
天命终不由人
天命终不由人 2020-12-09 00:22

I\'m working on an Android app, and I do not want people to use emoji in the input.

How can I remove emoji characters from a string?

4条回答
  •  春和景丽
    2020-12-09 00:56

    Here is what I use to remove emojis. Note: This only works on API 24 and forwards

    public  String remove_Emojis_For_Devices_API_24_Onwards(String name)
       {
        // we will store all the non emoji characters in this array list
         ArrayList nonEmoji = new ArrayList<>();
    
        // this is where we will store the reasembled name
        String newName = "";
    
        //Character.UnicodeScript.of () was not added till API 24 so this is a 24 up solution
        if (Build.VERSION.SDK_INT > 23) {
            /* we are going to cycle through the word checking each character
             to find its unicode script to compare it against known alphabets*/
            for (int i = 0; i < name.length(); i++) {
                // currently emojis don't have a devoted unicode script so they return UNKNOWN
                if (!(Character.UnicodeScript.of(name.charAt(i)) + "").equals("UNKNOWN")) {
                    nonEmoji.add(name.charAt(i));//its not an emoji so we add it
                }
            }
            // we then cycle through rebuilding the string
            for (int i = 0; i < nonEmoji.size(); i++) {
                newName += nonEmoji.get(i);
            }
        }
        return newName;
    }
    

    so if we pass in a string:

    remove_Emojis_For_Devices_API_24_Onwards("

提交回复
热议问题