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

Published by R0uter

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

Join the Conversation

2 Comments

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

    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
      }