Ios 如何检测字符串中的汉字

Ios 如何检测字符串中的汉字,ios,localization,nsstring,Ios,Localization,Nsstring,我有一个从服务器返回的NSString。在显示字符串之前,我需要检测它是否包含汉字 我尝试使用ASCII和UTF16或UTF8,但对所有字符串都返回true 是否有更好的解决方案?如中所述,您可以使用NSRegularExpression搜索\p{script=Han}: NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\p{script=Han}" options:NSRegula

我有一个从服务器返回的
NSString
。在显示字符串之前,我需要检测它是否包含汉字

我尝试使用ASCII和UTF16或UTF8,但对所有字符串都返回true

是否有更好的解决方案?

如中所述,您可以使用
NSRegularExpression
搜索
\p{script=Han}

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\p{script=Han}" options:NSRegularExpressionCaseInsensitive error:nil];
if ([regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])] > 0) {
    // string contains Chinese characters
}

这将检查字符串中是否有任何字符匹配
\p{script=Han}
,这是汉字Unicode范围的说明符。

您能显示您尝试过的代码吗?@stevekohls抱歉,我被删除了正在测试的代码。现在我尝试使用代码NSString*a=@”我们"; NSRegularExpression*regex=[NSRegularExpression regular expression with pattern:@“\p{script=Han}”选项:NSRegularExpression case不敏感错误:nil];if([regex numberofmatchesting:a选项:0范围:NSMakeRange(0,[a长度])]>0){NSLog(@“got chinese”);}其他{NSLog(@“no chinese”);}但它不起作用我尝试了NSString*a=@”我们“NSRegularExpression*regex=[NSRegularExpression regular expressionwithpattern:@“\p{script=Han}”选项:nsregularexpressioncase不敏感错误:nil];if([regex numberofmatchesisnstering:a选项:0范围:NSMakeRange(0,[a长度]])>0){NSLog(@“got chinse”);}else{NSLog(@“no chinese”);}但它不起作用。它对我来说仍然没有中文