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

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

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

你可能感兴趣的:(iOS 判断字符串是否是纯中文字符以及字符串的范围)