简体   繁体   English

如何在Swift中检查字符串是否包含中文?

[英]How can I check if a string contains Chinese in Swift?

I want to know that how can I check if a string contains Chinese in Swift? 我想知道如何在Swift中检查字符串是否包含中文?

For example, I want to check if there's Chinese inside: 例如,我想检查里面是否有中文:

var myString = "Hi! 大家好!It's contains Chinese!"

Thanks! 谢谢!

This answer to How to determine if a character is a Chinese character can also easily be translated from Ruby to Swift (now updated for Swift 3): 这个 如何判断字符是否是中文字符的 答案也可以很容易地从Ruby转换为Swift(现在更新为Swift 3):

extension String {
    var containsChineseCharacters: Bool {
        return self.range(of: "\\p{Han}", options: .regularExpression) != nil
    }
}

if myString.containsChineseCharacters {
    print("Contains Chinese")
}

In a regular expression, "\\p{Han}" matches all characters with the "Han" Unicode property, which – as I understand it – are the characters from the CJK languages. 在正则表达式中,“\\ p {Han}”匹配具有“Han”Unicode属性的所有字符,根据我的理解,它是CJK语言中的字符。

Looking at questions on how to do this in other languages (such as this accepted answer for Ruby) it looks like the common technique is to determine if each character in the string falls in the CJK range. 看看如何在其他语言中执行此操作的问题(例如Ruby的这个接受的答案 ),看起来常见的技术是确定字符串中的每个字符是否都在CJK范围内。 The ruby answer could be adapted to Swift strings as extension with the following code: 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

The ranges may already exist in Foundation somewhere rather than manually hardcoding them. 这些范围可能已存在于Foundation的某处,而不是手动对它们进行硬编码。

The above is for Swift 2.0, for earlier, you will have to use the free contains function rather than the protocol extension (twice): 以上是Swift 2.0,对于之前的版本,你必须使用free 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) }
        }
    }
}

Try this in Swift 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)")

The accepted answer only find if string contains Chinese character, i created one suit for my own case: 接受的答案只发现字符串是否包含中文字符,我为自己的情况创建了一个套装:

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")
}

gist here: https://gist.github.com/williamhqs/6899691b5a26272550578601bee17f1a 请点击这里: https//gist.github.com/williamhqs/6899691b5a26272550578601bee17f1a

I have created a Swift 3 String extension for checking how much Chinese characters a String contains. 我创建了一个Swift 3 String扩展,用于检查String包含多少个中文字符。 Similar to the code by Airspeed Velocity but more comprehensive. 类似于Airspeed Velocity的代码,但更全面。 Checking various Unicode ranges to see whether a character is Chinese. 检查各种Unicode范围以查看字符是否为中文。 See Chinese character ranges listed in the tables under section 18.1 in the Unicode standard specification: http://www.unicode.org/versions/Unicode9.0.0/ch18.pdf 请参阅Unicode标准规范中第18.1节表格中列出的中文字符范围: http//www.unicode.org/versions/Unicode9.0.0/ch18.pdf

The String extension can be found on GitHub: https://github.com/niklasberglund/String-chinese.swift String扩展可以在GitHub上找到: https//github.com/niklasberglund/String-chinese.swift

Usage example: 用法示例:

let myString = "Hi! 大家好!It contains Chinese!"
let chinesePercentage = myString.chinesePercentage()
let chineseCharacterCount = myString.chineseCharactersCount()
print("String contains \(chinesePercentage) percent Chinese. That's \(chineseCharacterCount) characters.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM