NSCharacterSet使用方法总结

需求中需要限制UITextField的输入和粘贴内容,特此总结相关用法,以备以后查询使用

1、常用的创建NSCharacterSet的类方法

系统提供的快捷创建方法:

//获取所选字符串以外的的内容
@property (readonly, copy) NSCharacterSet *invertedSet;//相反字符串限制

[NSCharacterSet controlCharacterSet];                 //控制符的字符集
[NSCharacterSet whitespaceCharacterSet];              //空格的字符集
[NSCharacterSet whitespaceAndNewlineCharacterSet];    //空格和换行符的字符集
[NSCharacterSet decimalDigitCharacterSet];            //十进制数字的字符集
[NSCharacterSet letterCharacterSet];                  //所有字母的字符集
[NSCharacterSet lowercaseLetterCharacterSet];         //小写字母的字符集
[NSCharacterSet uppercaseLetterCharacterSet];         //大写字母的字符集
[NSCharacterSet nonBaseCharacterSet];                 //非基础的字符集
[NSCharacterSet alphanumericCharacterSet];            //字母和数字的字符集
[NSCharacterSet decomposableCharacterSet];            //可分解
[NSCharacterSet illegalCharacterSet];                 //非法的字符集
[NSCharacterSet punctuationCharacterSet];             //标点的字符集
[NSCharacterSet capitalizedLetterCharacterSet];       //首字母大写的字符集
[NSCharacterSet symbolCharacterSet];                  //符号的字符集
[NSCharacterSet newlineCharacterSet];                 //换行符的字符集

基础创建方法:

//aRange中对应的是字符编码的数字
//NSMakeRange(48, 10),ascll码,此处为(0-9)
+(NSCharacterSet*)characterSetWithRange:(NSRange)aRange;
​
//aString中的文字组成的set
+(NSCharacterSet*)characterSetWithCharactersInString:(NSString*)aString; 
​
//(下面这两个没有用过,姑且写在这里吧)
//返回包含由给定位图表示形式确定的字符的字符集,此方法对于使用来自文件或其他外部数据源的数据创建字符集
+(NSCharacterSet*)characterSetWithBitmapRepresentation:(NSData*)data;
​
//返回从位图表示中读取的字符集,存储在文件中给定的路径
+(nullableNSCharacterSet*)characterSetWithContentsOfFile:(NSString*)fName;

 

//举例使用
//String中的文字组成的set 
[NSCharacterSetcharacterSetWithCharactersInString:@"Hello"];   
​
//ascll码,此处为(0-9) 
[NSCharacterSetcharacterSetWithRange:NSMakeRange(48, 10)]]; 
​
//数字之外的字符组成的set
[[NSCharacterSetdecimalDigitCharacterSet] invertedSet];
//decimalDigitCharacterSet 表示0-9的数字集合
//这里使用了invertedSet相反字符串限制
//指定aCharacter字符是包含于在于当前字符集
-(BOOL)characterIsMember:(unichar)aCharacter;
​
//使用方法
//48是0的ascll码
[[NSCharacterSetdecimalDigitCharacterSet] characterIsMember:48];  

2、NSMutableCharacter的方法

这些内容暂时没有用到,放在这里吧!


NSMutableCharacterSet *set1 = [NSMutableCharacterSet characterSetWithCharactersInString:@"Hell"];
NSMutableCharacterSet *set2 = [NSMutableCharacterSet characterSetWithCharactersInString:@"ello"];

//去掉某些字符
[set2 removeCharactersInString:@"e"]; //--->l, o

//加上某些字符
[set2 addCharactersInString:@"e"];    //--->e, l, o

//set相加
[set2 formUnionWithCharacterSet:set1]; //--->H,e,l,o

//本身加上另外一个的set相交
[set2 formIntersectionWithCharacterSet:set1]; //--->H,e,l

//除以包含的以外的set
[set2 invert];

3、UITextFile只能输入或者粘贴数字

在输入的时候可以通过键盘类型来限制输入的内容,但是粘贴的内容还是要通过以下的代码来处理


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
	[self restrictInputWithTextField:textField replacementString:string restrictString:@"0123456789"];
}	

//封装使用
//引入textField是为了进行更多的处理:例如限制第一个字符不能是0
- (BOOL)restrictInputWithTextField:(UITextField *)textField
                 replacementString:(NSString *)string 
                    restrictString:(NSString *)restrictString
{
    //例如限制第一个字符不能是0
    if ([textField.text isEqualToString:@"0"]) {
        return NO;
    }
    
	NSCharacterSet *charSet = [[NSCharacterSet characterSetWithCharactersInString:restrictString] invertedSet];
                        
    //将string按照charSet进行拆分,然后在拼接成新的字符串
    NSString *filteredStr = [[string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""];
                        
    if ([string isEqualToString:filteredStr]) 
    {
        return YES;
    }
    return NO;                          
}


//限制只能输入数字和小数点, 且第一位不可以输入小数点, 小数点只能输入一个
- (BOOL)restrictInputWithTextField:(UITextField *)textField
                 replacementString:(NSString *)string 
                     charactersInRange:(NSRange)range
{
    //开始位置不可以输入两个0
	if ([textField.text isEqualToString:@"0"] && [string isEqualToString:@"0"]) {
        return NO;
    }
	NSCharacterSet *cs;
    NSUInteger nDotLoc = [textField.text rangeOfString:@"."].location;
    if (NSNotFound == nDotLoc && 0 != range.location) 
    {
        cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
    }else{
        cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
    }
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    BOOL basicTest = [string isEqualToString:filtered];
    if (!basicTest) {
        return NO;
    }
    return YES;
}


4、NSString过滤

4.1、过滤空格

过滤NSString前后空格


NSString *str = @"  我是阿飞小伙子   ";
NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"处理之前|%@|, 处理之后|%@|", str, newStr);

//2018-11-21 12:04:28.767241+0800 characterSet[44398:1988171] 处理之前|  我是阿飞小伙子   |, 处理之后|我是阿飞小伙子|

过滤所有空格


NSString *str = @"  我是 阿飞 小伙子   ";
NSCharacterSet *charSet = [NSCharacterSet whitespaceCharacterSet];
                        
NSString *newStr = [[str componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""];

NSLog(@"处理之前|%@|, 处理之后|%@|", str, newStr);

//2018-11-21 12:05:30.238854+0800 characterSet[44439:1991387] 处理之前|  我是 阿飞 小伙子   |, 处理之后|我是阿飞小伙子|

4.2、过滤特殊字符

过滤特殊字符包括也包括空格。


//将要过滤的字符组成新的集合
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"@/:();()¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()_+'\""];

//然后就不啰嗦了

5、与NSString、NSArray的配合使用

//NSString中的相关方法
//字符串拆分,根据set内的字符拆分字符串  返回NSString*的数组
- (NSArray<NSString *> *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

//Returns a new string made by removing from both ends of the receiver characters contained in a given characterset.
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;

//looking for the searchString within the receiver string
- (NSRange)rangeOfString:(NSString *)searchString;


//NSArray相关方法
//将数组中的字符或者string字符串用separator拼接起来
- (NSString *)componentsJoinedByString:(NSString *)separator;

其他方法

下面方法使用较少,暂时放在这里

/* These return the range of the first character from the set in the string, not the range of a sequence of characters. 
 
The range argument specifies the subrange, rather than the whole, of the receiver to use in the search.  It is an error to specify a range that is outside of the receiver's bounds, and an exception may be raised.
*/
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet;
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet options:(NSStringCompareOptions)mask;
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch;

 

在这里也参考了一些大神的文章,在这里表示感谢!

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值