Skip to content

Instantly share code, notes, and snippets.

@williamhqs
Last active May 29, 2017 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williamhqs/6899691b5a26272550578601bee17f1a to your computer and use it in GitHub Desktop.
Save williamhqs/6899691b5a26272550578601bee17f1a to your computer and use it in GitHub Desktop.
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