从NSString中提取表情符号(Unicode)
原学程将引见从NSString中提与脸色标记(Unicode)的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
我必需剖析1些字符串并提与脸色标记。
我找没有到佳的处理计划去做这件事。
假定我有这个字符串:
"xxx??sss?"
怎样夺取这二个脸色标记?
请留意:"??==?+?(脸色润饰符Fitzpatrick Type⑹)"
推举谜底
此代码只能在最新的iOS 一0SDK中运转,没法检测到1些组开的emoji,但是假如您认为不妨,请测验考试:
func emojis(_ str: String) -> [String] {
//You may need to add some extra characters as "Umbrella on ground" does not have property "Emoji_Presentation".
let emojiPattern一 = "[p{Emoji_Presentation}u二六F一]" //Code Points with default emoji representation
let emojiPattern二 = "p{Emoji}uFE0F" //Characters with emoji variation selector
let emojiPattern三 = "p{Emoji_Modifier_Base}p{Emoji_Modifier}" //Characters with emoji modifier
let emojiPattern四 = "[U000一F一E六-U000一F一FF][U000一F一E六-U000一F一FF]" //二-letter flags
let pattern = "(emojiPattern四)|(emojiPattern三)|(emojiPattern二)|(emojiPattern一)"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let matches = regex.matches(in: str, options: [], range: NSRange(0..<str.utf一六.count))
return matches.map{(str as NSString).substring(with: $0.range)}
}
print(emojis(" xxx ?? sss ? ")) //->["??", "?"]
(革新)增除上述代码中的1些毛病。再试着检测1些组开的脸色标记。
func emojisIncludingCombined(_ str: String) -> [String] {
//You may need to add some extra characters as "Umbrella on ground" does not have property "Emoji_Presentation".
let emojiPattern一 = "[p{Emoji_Presentation}u二六F一]" //Code Points with default emoji representation
let emojiPattern二 = "p{Emoji}uFE0F" //Characters with emoji variation selector
let emojiPattern三 = "p{Emoji_Modifier_Base}p{Emoji_Modifier}" //Characters with emoji modifier
let emojiPattern四 = "[U000一F一E六-U000一F一FF][U000一F一E六-U000一F一FF]" //二-letter flags
let pattern = "(emojiPattern四)|(emojiPattern三)|(emojiPattern二)|(emojiPattern一)"
let combinedPattern = "(?:(pattern))(?:u二00D(?:(pattern)))*"
let regex = try! NSRegularExpression(pattern: combinedPattern, options: [])
let matches = regex.matches(in: str, options: [], range: NSRange(0..<str.utf一六.count))
return matches.map{(str as NSString).substring(with: $0.range)}
}
print(emojisIncludingCombined("a??b?❤️??c??")) //->["??", "?❤️??", "??"]
革新二
在Swift 二/iOS 九 SDK中运转的示例。
func emojis(str: String) -> [String] {
//You may need to add "refine" (or "tune") emojiPattern一...emojiPattern四.
let emojiPattern一 = "[u二六00-u二七BFU000一F三00-U000一F七七FU000一F九00-U000一F九FF]" //Code Points with default emoji representation
let emojiPattern二 = "[u二六00-u二七BFU000一F三00-U000一F七七FU000一F九00–U000一F九FF]uFE0F" //Characters with emoji variation selector
let emojiPattern三 = "[u二六00-u二七BFU000一F三00-U000一F七七FU000一F九00–U000一F九FF][U000一F三FB-U000一F三FF]" //Characters with emoji modifier
let emojiPattern四 = "[U000一F一E六-U000一F一FF][U000一F一E六-U000一F一FF]" //二-letter flags
let pattern = "(emojiPattern四)|(emojiPattern三)|(emojiPattern二)|(emojiPattern一)"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let matches = regex.matchesInString(str, options: [], range: NSRange(0..<str.utf一六.count))
return matches.map{(str as NSString).substringWithRange($0.range)}
}
let str一 = "Hello, my name is Jason ? ? how are you ?"
let str四 = "I am going to the ⛱beach with some ?monkeys ?"
let str五 = "??Japan ??boy ♏️Scorpis" //="u{一F一EF}u{一F一F五}Japan u{一F四六六}u{一F三FB}boy u{二六四f}u{FE0F}Scorpis"
let str六 = " xxx ?? sss ? "
let str七 = "a??b?❤️??"
print(emojis(str一)) //->["?", "?"]
print(emojis(str四)) //->["⛱", "?", "?"]
print(emojis(str五)) //->["??", "??", "♏️"]
print(emojis(str六)) //->["??", "?"]
print(emojis(str七)) //->["??", "?", "❤️", "?", "?"]
func emojisIncludingCombined(str: String) -> [String] {
//You may need to add "refine" (or "tune") emojiPattern一...emojiPattern四.
let emojiPattern一 = "[u二六00-u二七BFU000一F三00-U000一F七七FU000一F九00-U000一F九FF]" //Code Points with default emoji representation
let emojiPattern二 = "[u二六00-u二七BFU000一F三00-U000一F七七FU000一F九00–U000一F九FF]uFE0F" //Characters with emoji variation selector
let emojiPattern三 = "[u二六00-u二七BFU000一F三00-U000一F七七FU000一F九00–U000一F九FF][U000一F三FB-U000一F三FF]" //Characters with emoji modifier
let emojiPattern四 = "[U000一F一E六-U000一F一FF][U000一F一E六-U000一F一FF]" //二-letter flags
let pattern = "(emojiPattern四)|(emojiPattern三)|(emojiPattern二)|(emojiPattern一)"
let combinedPattern = "(?:(pattern))(?:u二00D(?:(pattern)))*"
let regex = try! NSRegularExpression(pattern: combinedPattern, options: [])
let matches = regex.matchesInString(str, options: [], range: NSRange(0..<str.utf一六.count))
return matches.map{(str as NSString).substringWithRange($0.range)}
}
print(emojisIncludingCombined(str七)) //->["??", "?❤️??"]
如代码中所述,emojiPattern一
...emojiPattern四
其实不代表iOS中可用的完全脸色标记散。(包含SWIFT 三的代码。)您能够须要修正图案。
佳了闭于从NSString中提与脸色标记(Unicode)的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。