-(BOOL)isChinese:(NSString *)str
{
for(int i=0; i< [str length];i++)
{
int a = [str characterAtIndex:i];
if( a > 0x4e00 && a < 0x9fff)
{
return YES;
}
}
return NO;
}
- (BOOL)isChinese2:(NSString *)str
{
NSString *match = @"(^[\u4e00-\u9fa5]+$)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
return [predicate evaluateWithObject:str];
}
NSString *str = @"张是多少是否为";
NSString *str1 = @"张是多少一一";
NSString *str2 = @"一一一一一";
NSLog(@"%@%@中文",str,[self isChinese:str]?@"是":@"不是");
NSLog(@"%@%@中文",str1,[self isChinese:str1]?@"是":@"不是");
NSLog(@"%@%@中文",str2,[self isChinese:str2]?@"是":@"不是");
NSLog(@"%@%@中文",str,[self isChinese2:str]?@"是":@"不是");
NSLog(@"%@%@中文",str1,[self isChinese2:str1]?@"是":@"不是");
NSLog(@"%@%@中文",str2,[self isChinese2:str2]?@"是":@"不是");
打印结果
2019-06-27 14:54:48.516569+0800 chinese[21742:531673] 是否为是中文
2019-06-27 14:54:48.516699+0800 chinese[21742:531673] 张一一是中文
2019-06-27 14:54:48.516787+0800 chinese[21742:531673] 一一一不是中文
2019-06-27 14:54:48.520455+0800 chinese[21742:531673] 是否为是中文
2019-06-27 14:54:48.520620+0800 chinese[21742:531673] 张一一是中文
2019-06-27 14:54:48.520700+0800 chinese[21742:531673] 一一一是中文
网友评论