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

xiaoxiao2021-02-28  88

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

+ (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; } }
转载请注明原文地址: https://www.6miu.com/read-32901.html

最新回复(0)