美文网首页
[010]Swift 除去String中的emoji表情

[010]Swift 除去String中的emoji表情

作者: 快乐捣蛋鬼 | 来源:发表于2019-10-16 10:39 被阅读0次

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!

相关文章

网友评论

      本文标题:[010]Swift 除去String中的emoji表情

      本文链接:https://www.haomeiwen.com/subject/gjyymctx.html