用于简单过滤字符串中的一些特定字符。

Reference

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 控制符
+ (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 NS_AVAILABLE(10_5, 2_0);

whitespaceCharacterSet

Example

1
2
3
4
5
6
NSString *aString = @"  Neil Zhou's  Blog ";
NSLog(@"aString.length = %@", @(aString.length));
NSLog(@"aString = %@", aString);
NSString *bString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"bString.length = %@", @(bString.length));
NSLog(@"bString = %@", bString);

Output

1
2
3
4
2016-07-10 22:22:26.900 NSCharacterSet[11964:8282126] aString.length = 20
2016-07-10 22:22:26.902 NSCharacterSet[11964:8282126] aString = Neil Zhou's Blog
2016-07-10 22:22:26.902 NSCharacterSet[11964:8282126] bString.length = 17
2016-07-10 22:22:26.902 NSCharacterSet[11964:8282126] bString = Neil Zhou's Blog

lowercaseLetterCharacterSet

Example

1
2
NSRange lowercaseCharRange = [@"abc" rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]];
NSLog(@"%d", lowercaseCharRange.location!=NSNotFound);

Output

1
2016-07-10 22:58:43.986 NSCharacterSet[12362:8503816] 1

uppercaseLetterCharacterSet

Example

1
2
3
NSString *string = @"Talk is cheap, show me the code.";
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[string characterAtIndex:0]];
NSLog(@"%d", isUppercase);

Output

1
2016-07-10 22:56:29.389 NSCharacterSet[12350:8488247] 1

punctuationCharacterSet

Example

1
2
3
4
5
6
7
unichar zyy_unichar1 = [@"A,;" characterAtIndex:0];
unichar zyy_unichar2 = [@"A,;" characterAtIndex:1];
unichar zyy_unichar3 = [@"A,;" characterAtIndex:2];
NSCharacterSet *characterSet = [NSCharacterSet punctuationCharacterSet];
NSLog(@"%d",[characterSet characterIsMember:zyy_unichar1]);
NSLog(@"%d",[characterSet characterIsMember:zyy_unichar2]);
NSLog(@"%d",[characterSet characterIsMember:zyy_unichar3]);

Output

1
2
3
2016-07-10 22:48:41.903 NSCharacterSet[12291:8434819] 0
2016-07-10 22:48:41.904 NSCharacterSet[12291:8434819] 1
2016-07-10 22:48:41.904 NSCharacterSet[12291:8434819] 1