简体   繁体   中英

How to remove emoji from string - ios?

I've seen many of the answers are based on the unicode of the character and the unicode of the emodji range, but the emoji is added every year, it is difficult to maintain. Is there a simpler way to implement it?

Actually there seems to be a very easy solution to this:

func removeEmojisFromString(_ string: String) -> String {
    string.unicodeScalars.filter { !($0.properties.isEmoji && $0.properties.isEmojiPresentation) }.map { String($0) }.joined()
}

Tested with

let filtered = removeEmojisFromString("This is actually pretty easy 🤣 iOS has a native categorization for emoji? 😳")
print(filtered)

and the result is This is actually pretty easy iOS has a native categorization for emoji?

There are some extra spaces you might need to remove as well but that is another thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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