Description:
給出一個字符串,去掉字符串中的emoji表情
Example:
var testString = "??OMG Cute???baby pictures!?? ??"
testString = removeEmojis(testString)
print(testString) // OMG Cutebaby pictures!
Solution:
func removeEmojis(_ string: String) -> String {
var scalars = string.unicodeScalars
scalars.removeAll(where: isEmoji)
return String(scalars)
}
func isEmoji(_ scalar: Unicode.Scalar) -> Bool {
switch Int(scalar.value) {
case 0x1F601...0x1F64F: return true // Emoticons
case 0x1F600...0x1F636: return true // Additional emoticons
case 0x2702...0x27B0: return true // Dingbats
case 0x1F680...0x1F6C0: return true // Transport and map symbols
case 0x1F681...0x1F6C5: return true // Additional transport and map symbols
case 0x24C2...0x1F251: return true // Enclosed characters
case 0x1F30D...0x1F567: return true // Other additional symbols
default: return false
}
}
var testString = "??OMG Cute???baby pictures!?? ??"
testString = removeEmojis(testString)
print(testString) // OMG Cutebaby pictures!