iOS:字符串中是否含有中文

来源:互联网 发布:魔方虚拟光驱软件 编辑:程序博客网 时间:2024/05/17 23:29

判断字符串中是否含有中文

+ (BOOL)isIncludeChineseInString:(NSString*)str {    for (int i=0; i<str.length; i++) {        unichar ch = [str characterAtIndex:i];        if (0x4E00 <= ch  && ch <= 0x9FA5) {            return YES;        }    }    return NO;}

错误:在网上很多都写的这样的代码,如下(字符串”一”即可检验返回值):

-(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;}

备注:因为中文代码范围0x4E00~0x9FA5,
不信的话可以看看文档 unicode编码表

0 0