Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

For example, I want to check if there's Chinese inside:

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

Thanks!

like image 800
He Yifei 何一非 Avatar asked Jul 06 '15 11:07

He Yifei 何一非


People also ask

How do I check if a string contains something in Swift?

Swift String contains() The contains() method checks whether the specified string (sequence of characters) is present in the string or not.

How do you check if a character is in a string Swift?

In the Swift string, we check if the specified string is present in a string or not. To do this task we use the contains() function. This function is used to check whether the specified string i.e. sequence of characters is present in the string or not.

Is there a character type in Swift?

Swift CharacterCharacter is a data type that represents a single-character string ( "a" , "@" , "5" , etc). Here, the letter variable can only store single-character data.


3 Answers

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):

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.

like image 68
Martin R Avatar answered Nov 15 '22 15:11

Martin R


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. The ruby answer could be adapted to Swift strings as extension with the following code:

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.

The above is for Swift 2.0, for earlier, you will have to use the free contains function rather than the protocol extension (twice):

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) }
        }
    }
}
like image 45
Airspeed Velocity Avatar answered Nov 15 '22 16:11

Airspeed Velocity


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

like image 21
William Hu Avatar answered Nov 15 '22 17:11

William Hu