Swift is determined whether there is a string expression Emoji

Update:The network spread emoji Code pointNot complete,I follow Wikipedia-refresh a bit,The paper version of the code was updated Swift。


Many times we need to determine a character、Or that there is not a word that contains the emoji,Use Swift language development app is no exception,For example, you can use a regular expression - but unfortunately,It seems different language support for regular expressions distinction, \in This feature is not so that's where all,So the most simple and crude way,We get a string,It contains all that may be used emoji,Then to retrieve it:

In fact, it,Be conscience Kazakhstan,Of course, the efficiency is also good ......,The result is that you have to constantly update the list - and you have to put the card was a force Xcode state。

Use traversal

so,There is a medium with OC achieve extended,It seems very wide spread:

In fact, its principle is to match the range where the Emoji,so,If we apply the Swift statements and their characteristics,So is this version Swift:

?Is not it simple?

Code from:Check if Swift string contains an emoji or dingbat charater

Original article written by LogStudio:R0uter's Blog » Swift is determined whether there is a string expression Emoji

Reproduced Please keep the source and description link:https://www.logcg.com/archives/2504.html

About the Author

R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Comments

    1. The same is true with the regular principle,Match emoji interval range,Can use regular。When I was writing this article, I did n’t think I could use regular,I'm not skilled at this。
      But because emoji has been evolving,The final result is that I chose to store emoji separately and record the data source,Fundamentally distinguished them,No need to judge。(Of course this is for my own business logic)
      Since emoji is longer,I think it's easier to judge ASCII characters in reverse,If it contains Chinese, add the Chinese character interval.。

    1. Yes,This does not cover all range of emoji,Because the emoji range itself is an evolution of the enlarged,You want to change the range of values ​​of the change,For example, I added the following code,This is not a few in the body that range。
      if (self.range(of: "8️⃣") != nil) {                return true            } else if (self.range(of: "0️⃣") != nil) {                return true            } else if (self.range(of: "6️⃣") != nil) {                return true            }

Leave a Reply

Your email address will not be published. Required fields are marked *