Last active
May 29, 2017 03:50
-
-
Save williamhqs/6899691b5a26272550578601bee17f1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum ChineseRange { | |
case notFound, contain, all | |
} | |
extension String { | |
var findChineseCharacters: ChineseRange { | |
guard let a = self.range(of: "\\p{Han}*\\p{Han}", options: .regularExpression) else { | |
return .notFound | |
} | |
var result: ChineseRange | |
switch a { | |
case nil: | |
result = .notFound | |
case self.startIndex..<self.endIndex: | |
result = .all | |
default: | |
result = .contain | |
} | |
return result | |
} | |
} | |
if "你好".findChineseCharacters == .all { | |
print("All Chinese") | |
} | |
if "chinese".findChineseCharacters == .notFound { | |
print("Not found chinese") | |
} | |
if "chinese你好".findChineseCharacters == .contain { | |
print("Contains Chinese") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment