Created
August 30, 2013 06:31
-
-
Save zhangwc/6386852 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//函数描述: string 字数限制 | |
//输入参数:string:要判断的string ;minSize:最小字数限制;maxSize:最大字数限制 | |
//返回值: BOOL:是否超出限制 | |
//备注:一个汉字占两个字符,其他字母占一个字符 | |
+ (BOOL)checkStringSize:(NSString*)string minSize:(int)minSize maxSize:(int)maxSize | |
{ | |
NSString *regex = @"^[\u4e00-\u9fa5]"; | |
int length=[string length]; | |
int currentStringSize=0; | |
for (int i=0;i<length;i++) {//逐步判断string中的每个字符是否为汉字,是,占两个字符,不是,占一个字符 | |
NSString *subString= [string substringWithRange:NSMakeRange( i, 1)] ; | |
if ([subString isMatchedByRegex:regex]) { | |
currentStringSize+=2; | |
}else { | |
currentStringSize+=1; | |
} | |
} | |
if (currentStringSize>=minSize&¤tStringSize<=maxSize) { | |
return YES; | |
}else { | |
return NO; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment