NSCharacterSet ,以及它的可变类型 NSMutableCharacterSet,与NSString组合起来使用,在不同的字符串上做过滤、删除或者分割操作。
用的最多的地方是对字符串进行前后空格以及换行符的过滤。
如:
- (NSString *)stringByTrim {
NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];//空格和换行符的字符集
//NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet];//空格的字符集
return [self stringByTrimmingCharactersInSet:set];
}
过滤前后指定的字符串:
- (NSString *)stringByTrimFilterStr:(NSString *)filterStr {
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:filterStr];
return [self stringByTrimmingCharactersInSet:set];
}
过滤字符串中所有的空格:
- (NSString *)stringByTrimAllWhitespace {
NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet];//空格的字符集
return [[self componentsSeparatedByCharactersInSet:set] componentsJoinedByString:@""];
}
用指定字符来替换字符串中的字符:
- (NSString *)replaceWithStr:(NSString *)replaceedStr withStr:(NSString *)str{
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:replaceedStr];
return [[self componentsSeparatedByCharactersInSet:set] componentsJoinedByString:str];
}
另外可以对输入框做输入限制,如:
//限制只能输入数字和字母
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
NSCharacterSet*cs = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] invertedSet];
NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOL basicTest = [string isEqualToString:filtered];
return basicTest;
}
相关的属性和方法有:
@property (readonly, class, copy) NSCharacterSet *controlCharacterSet;//控制符的字符集
@property (readonly, class, copy) NSCharacterSet *whitespaceCharacterSet;//空格的字符集
@property (readonly, class, copy) NSCharacterSet *whitespaceAndNewlineCharacterSet;//空格和换行符的字符集
@property (readonly, class, copy) NSCharacterSet *decimalDigitCharacterSet;//十进制数字的字符集
@property (readonly, class, copy) NSCharacterSet *letterCharacterSet; //字母的字符集
@property (readonly, class, copy) NSCharacterSet *lowercaseLetterCharacterSet;//小写字母的字符集
@property (readonly, class, copy) NSCharacterSet *uppercaseLetterCharacterSet;//大写字母的字符集
@property (readonly, class, copy) NSCharacterSet *nonBaseCharacterSet;//非基础的字符集
@property (readonly, class, copy) NSCharacterSet *alphanumericCharacterSet;//字母和数字的字符集
@property (readonly, class, copy) NSCharacterSet *decomposableCharacterSet;//可分解
@property (readonly, class, copy) NSCharacterSet *illegalCharacterSet;//非法的字符集
@property (readonly, class, copy) NSCharacterSet *punctuationCharacterSet;//标点的字符集
@property (readonly, class, copy) NSCharacterSet *capitalizedLetterCharacterSet;//首字母大写的字符集
@property (readonly, class, copy) NSCharacterSet *symbolCharacterSet;//符号的字符集
@property (readonly, class, copy) NSCharacterSet *newlineCharacterSet; //换行符的字符集
@property (readonly, copy) NSCharacterSet *invertedSet;//反转字符集,仅包含当前字符集中不存在的字符
+ (NSCharacterSet *)characterSetWithRange:(NSRange)aRange;//返回一个指定范围的字符集,取自小写字母字符集
+ (NSCharacterSet *)characterSetWithCharactersInString:(NSString *)aString;//返回一个包含当前字符串的字符集
+ (NSCharacterSet *)characterSetWithBitmapRepresentation:(NSData *)data;//返回包含由给定位图表示形式确定的字符的字符集,此方法对于使用来自文件或其他外部数据源的数据创建字符集
+ (nullable NSCharacterSet *)characterSetWithContentsOfFile:(NSString *)fName;//返回从位图表示中读取的字符集,存储在文件中给定的路径。
- (BOOL)characterIsMember:(unichar)aCharacter;//指定字符集是包含于在于当前字符集