[iOS]判斷字串是否有中文

紀錄一下中文的比對是\u4e00-\u9fa5

-(BOOL)isChineseCharacter:(NSString*)searchText {
    searchText = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *searchRegex = @"[\u4e00-\u9fa5]";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", searchRegex];
    BOOL matches = [predicate evaluateWithObject:searchText];
    return matches;
}

參考資料
[iOS] 正規表達式 (Regular Expression)
Objective-C笔记之正则表达式

沒有留言: