NSCharacterSet-字符集使用

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 (readonlycopyNSCharacterSet *invertedSet;//反转字符集,仅包含当前字符集中不存在的字符

+ (NSCharacterSet *)characterSetWithRange:(NSRange)aRange;//返回一个指定范围的字符集,取自小写字母字符集

+ (NSCharacterSet *)characterSetWithCharactersInString:(NSString *)aString;//返回一个包含当前字符串的字符集

+ (NSCharacterSet *)characterSetWithBitmapRepresentation:(NSData *)data;//返回包含由给定位图表示形式确定的字符的字符集,此方法对于使用来自文件或其他外部数据源的数据创建字符集

+ (nullable NSCharacterSet *)characterSetWithContentsOfFile:(NSString *)fName;//返回从位图表示中读取的字符集,存储在文件中给定的路径。


- (BOOL)characterIsMember:(unichar)aCharacter;//指定字符集是包含于在于当前字符集


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值