iOS 判断字符串是否是纯中文字符以及字符串的范围
程序员文章站
2022-05-01 17:46:00
判断一个字符串是否是纯中文字符,代码如下
+ (bool)ischinese:(nsstring *)username
{
nsstring *match = @&qu...
判断一个字符串是否是纯中文字符,代码如下
+ (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; } }