Skip to content

Instantly share code, notes, and snippets.

@heronlyj
Last active October 6, 2016 03:14
Show Gist options
  • Save heronlyj/40f10d1089aa7cff67f5 to your computer and use it in GitHub Desktop.
Save heronlyj/40f10d1089aa7cff67f5 to your computer and use it in GitHub Desktop.
Swift Separate Chinese Character
extension Character {
var scalrValue: UInt32 {
return String(self).unicodeScalars.first!.value
}
var isChineseCharacter: Bool {
switch self.scalrValue {
case 0x4E00...0x9FFF, 0x3400...0x4DBF, 0x20000...0x2A6DF, 0x2A700...0x2B73F: return true
default: return false
}
}
var isEnglishCharacter: Bool {
switch self.scalrValue {
case 0x0061...0x007A, 0x0041...0x005A: return true
default: return false
}
}
}
extension String {
var containsChineseCharacters: Bool {
return self.characters.filter{$0.isChineseCharacter}.count != 0
}
var mapForChineseCharacters: [Character] {
return self.characters.filter{ $0.isChineseCharacter }
}
var mapForEnglishCharacters: [Character] {
return self.characters.filter{ $0.isEnglishCharacter }
}
var mapBeyondCECharacters: [Character] {
return self.characters.filter { !($0.isEnglishCharacter || $0.isChineseCharacter)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment