当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS 判断字符串是否是纯中文字符以及字符串的范围

iOS 判断字符串是否是纯中文字符以及字符串的范围

2018年09月12日  | 移动技术网移动技术  | 我要评论

判断一个字符串是否是纯中文字符,代码如下

 

+ (bool)ischinese:(nsstring *)username
{
    nsstring *match = @"(^[\u4e00-\u9fa5]+$)";
    nspredicate *predicate = [nspredicate predicatewithformat:@"self matches %@", match];
    return [predicate evaluatewithobject:username];
}

 

判断一个字符串是否在某个范围 比如2~15个汉字,代码如下

 

 

+ (bool)isvalidatename:(nsstring *)name{
    nsuinteger  character = 0;
    for(int i=0; i< [name length];i++){
        int a = [name characteratindex:i];
        if( a >= 0x4e00 && a <= 0x9fff){ //判断是否为中文
            character +=2;
        }else{
            character +=1;
        }
    }
    log(@"%ld",character);
    if (character >=4 && character <=30) {
        return yes;
    }else{
        return no;
    }
    
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网