iOS 判断字母、数字串

来源:互联网 发布:linux无图形系统 编辑:程序博客网 时间:2024/05/14 13:59

以下为NSString的扩展方法,分别是判断字符串是否只是包含字母、是否只包含数字、是否只包含字母和数字:

//只有字母- (BOOL)isOnlyLetters {  NSCharacterSet *letterCharacterset = [[NSCharacterSet letterCharacterSet] invertedSet];  return ([self rangeOfCharacterFromSet:letterCharacterset].location == NSNotFound);}//只有数字- (BOOL)isOnlyNumbers {  NSCharacterSet *numSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];  return ([self rangeOfCharacterFromSet:numSet].location == NSNotFound);}//字母和数字- (BOOL)isOnlyAlphaNumeric {  NSCharacterSet *numAndLetterCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];  return ([self rangeOfCharacterFromSet:numAndLetterCharSet].location == NSNotFound);}
0 0
原创粉丝点击