Swift - Replacing emojis in a string with whitespace

前端 未结 6 1410
悲&欢浪女
悲&欢浪女 2020-12-18 11:05

I have a method that detects urls in a string and returns me both the urls and the ranges where they can be found. Everything works perfectly until there are emojis on the s

6条回答
  •  既然无缘
    2020-12-18 11:26

    Swift 4:

    extension String {
      func stringByRemovingEmoji() -> String {
        return String(self.filter { !$0.isEmoji() })
      }
    }
    
    extension Character {
      fileprivate func isEmoji() -> Bool {
        return Character(UnicodeScalar(UInt32(0x1d000))!) <= self && self <= Character(UnicodeScalar(UInt32(0x1f77f))!)
          || Character(UnicodeScalar(UInt32(0x2100))!) <= self && self <= Character(UnicodeScalar(UInt32(0x26ff))!)
      }
    }
    

提交回复
热议问题