NSCharacterSet
表示用于搜索操作的一组固定Unicode字符值的对象。
字符串处理的例子
NSString *string = @"1212erd35446dfd456";
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"erdf"];
NSArray *setArray = [string componentsSeparatedByCharactersInSet:characterSet];
NSString *resultString = [setArray componentsJoinedByString:@""];
输出:121235446456
+ controlCharacterSet
+ whitespaceCharacterSet //空格
+ whitespaceAndNewlineCharacterSet //空格和换行符
+ decimalDigitCharacterSet //0-9的数字
+ letterCharacterSet //所有字母
+ lowercaseLetterCharacterSet //小写字母
+ uppercaseLetterCharacterSet //大写字母
+ alphanumericCharacterSet //所有数字和字母(大小写不分)
+ punctuationCharacterSet //标点符号
+ newlineCharacterSet //换行
textfield只可以输入数字
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *charSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
NSString *str = [[string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""];
if ([string isEqualToString:str])
{
return YES;
}
else
{
return NO;
}
}