我想知道如何在Swift中检查字符串是否包含中文?
例如,我想检查里面是否有中文:
{{1}}
谢谢!
答案 0 :(得分:9)
This answer How to determine if a character is a Chinese character也可轻松翻译成{{3}} Ruby to Swift(现在更新为Swift 3):
extension String {
var containsChineseCharacters: Bool {
return self.range(of: "\\p{Han}", options: .regularExpression) != nil
}
}
if myString.containsChineseCharacters {
print("Contains Chinese")
}
在正则表达式中,“\ p {Han}”匹配所有字符 “Han”Unicode属性,正如我所理解的那样 - 是角色 来自CJK语言。
答案 1 :(得分:5)
查看有关如何在其他语言中执行此操作的问题(例如Ruby的this accepted answer),看起来常见的技术是确定字符串中的每个字符是否都在CJK范围内。 ruby的答案可以通过以下代码适应Swift字符串作为扩展名:
extension String {
var containsChineseCharacters: Bool {
return self.unicodeScalars.contains { scalar in
let cjkRanges: [ClosedInterval<UInt32>] = [
0x4E00...0x9FFF, // main block
0x3400...0x4DBF, // extended block A
0x20000...0x2A6DF, // extended block B
0x2A700...0x2B73F, // extended block C
]
return cjkRanges.contains { $0.contains(scalar.value) }
}
}
}
// true:
"Hi! 大家好!It's contains Chinese!".containsChineseCharacters
// false:
"Hello, world!".containsChineseCharacters
这些范围可能已存在于Foundation的某处,而不是手动对其进行硬编码。
以上是针对Swift 2.0的,对于之前的版本,您必须使用免费的contains
函数而不是协议扩展(两次):
extension String {
var containsChineseCharacters: Bool {
return contains(self.unicodeScalars) {
// older version of compiler seems to need extra help with type inference
(scalar: UnicodeScalar)->Bool in
let cjkRanges: [ClosedInterval<UInt32>] = [
0x4E00...0x9FFF, // main block
0x3400...0x4DBF, // extended block A
0x20000...0x2A6DF, // extended block B
0x2A700...0x2B73F, // extended block C
]
return contains(cjkRanges) { $0.contains(scalar.value) }
}
}
}
答案 2 :(得分:2)
在Swift 2中试试这个:
var myString = "Hi! 大家好!It's contains Chinese!"
var a = false
for c in myString.characters {
let cs = String(c)
a = a || (cs != cs.stringByApplyingTransform(NSStringTransformMandarinToLatin, reverse: false))
}
print("\(myString) contains Chinese characters = \(a)")
答案 3 :(得分:1)
接受的答案只能找到字符串是否包含中文字符,我为自己的情况创建了一个套装:
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")
}
请点击此处:https://gist.github.com/williamhqs/6899691b5a26272550578601bee17f1a
答案 4 :(得分:0)
我创建了一个Swift 3 String扩展,用于检查String包含的中文字符数。类似于Airspeed Velocity的代码,但更全面。检查各种Unicode范围以查看字符是否为中文。请参阅Unicode标准规范中第18.1节表格中列出的中文字符范围:http://www.unicode.org/versions/Unicode9.0.0/ch18.pdf
可以在GitHub上找到String扩展名:https://github.com/niklasberglund/String-chinese.swift
用法示例:
let myString = "Hi! 大家好!It contains Chinese!"
let chinesePercentage = myString.chinesePercentage()
let chineseCharacterCount = myString.chineseCharactersCount()
print("String contains \(chinesePercentage) percent Chinese. That's \(chineseCharacterCount) characters.")