我需要检查字符串的所有字符的 ascii 值是否存在于给定范围内,比如(65 到 90),该方法应该根据测试返回 true 或 false,我如何在 Objective-c 中实现这一点?
问问题
363 次
1 回答
1
Try this one:
Call the method as :
NSLog(@"Result: %d",[self isString:string fromASCII:65 toASCII:90]);
//Result: 1, says it is a valid string as per given range of ascii, and 0 for invalid.
The method is:
-(BOOL)isString:(NSString *)string fromASCII:(NSInteger)from toASCII:(NSInteger)to{
BOOL isValid=YES;
for (NSInteger i=0; i<string.length; i++) {
if (!([string characterAtIndex:i]>=from && [string characterAtIndex:i]<=to) ){
isValid=NO;
break;
}
}
return isValid;
}
于 2013-01-21T07:19:21.747 回答