NSCharacter​Set在字符串操作中得使用

NSCharacterSet以及它的可变版本NSMutableCharacterSet,用面向对象的方式表示一组Unicode字符,它经常与NSString及NSScanner组合起来使用,在不同的字符上做过滤,删除或者分割操作,下面是NSCharacterSet提供的类方法

Creating a Standard Character Set + alphanumericCharacterSet

+ capitalizedLetterCharacterSet

+ controlCharacterSet

+ decimalDigitCharacterSet

+ decomposableCharacterSet

+ illegalCharacterSet

+ letterCharacterSet

+ lowercaseLetterCharacterSet

+ newlineCharacterSet

+ nonBaseCharacterSet

+ punctuationCharacterSet

+ symbolCharacterSet

+ uppercaseLetterCharacterSet

+ whitespaceAndNewlineCharacterSet

+ whitespaceCharacterSet

Creating a Character Set for URL Encoding + URLFragmentAllowedCharacterSet

+ URLHostAllowedCharacterSet

+ URLPasswordAllowedCharacterSet

+ URLPathAllowedCharacterSet

+ URLQueryAllowedCharacterSet

+ URLUserAllowedCharacterSet

Creating a Custom Character Set + characterSetWithCharactersInString:

+ characterSetWithRange:

– invertedSet

Creating and Managing Character Sets as Bitmap Representations + characterSetWithBitmapRepresentation:

+ characterSetWithContentsOfFile:

– bitmapRepresentation

Testing Set Membership

– characterIsMember:

– hasMemberInPlane:

– isSupersetOfSet:

– longCharacterIsMember:

去掉空格

NSString -stringByTrimmingCharactersInSet: 是个你需要牢牢记住的方法。它经常会传入NSCharacterSet +whitespaceCharacterSet 或 +whitespaceAndNewlineCharacterSet 来删除输入字符串的头尾的空白符号。

1 //除去字符串两端的空格    

2 +(NSString *) trimStringWhitespace:(NSString *)str{

3     NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];

4     str = [str stringByTrimmingCharactersInSet:whitespace];

5     return str;

6 }
 1 //判断一个字符串是否只包含数字

 2 +(BOOL)isTureCustomNum:(NSString *)customNum

 3 {

 4     NSCharacterSet *nameCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];

 5     NSRange userNameRange = [customNum rangeOfCharacterFromSet:nameCharacters];

 6     if (userNameRange.location != NSNotFound) {

 7         //包含特殊字符

 8         return NO;

 9     }else{

10         return YES;

11     }

12 

13 }

14 

15 

16 

17 //判断一个字符串是否只包含字母 、数字和下划线

18 +(BOOL)isValidateString:(NSString *)str{

19     NSCharacterSet *nameCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"] invertedSet];

20     NSRange userNameRange = [str rangeOfCharacterFromSet:nameCharacters];

21     if (userNameRange.location != NSNotFound) {

22         //包含特殊字符

23         return NO;

24     }else{

25         return YES;

26     }

27 }

 

这个方法仅仅除去了开头和结尾空白符号,如果你想除去单词之间的额外空格,看下面的一个方法

1     NSString *string = @"Lorem    ipsum dolar   sit  amet.";

2     string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

3     

4     NSArray *components = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

5     components = [components filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self <> ''"]];

6     

7     string = [components componentsJoinedByString:@" "];

首先,删除字符串收尾的空格,然后用NSString -componentsSeparatedByCharactersInSet: 在空格处将字符串分割成一个 NSArray;再用一个 NSPredicate 去除空串;最后,用 NSArray -componentsJoinedByString: 用单个空格符将数组重新拼成字符串。注意:这种方法仅适用于英语这种用空格分割的语言。

不要用 NSCharacterSet 来分词。  CFStringTokenizer 来替代它

语言并不是都用空格作为词的分界。虽然实际上以空格分界的语言使用非常广泛。

……即使是用空格分隔的语言,分词也有一些模棱两可的边界条件,特别是复合词汇和标点符号。

以上只为说明:如果你想将字符串分成有意义的单词,那么请用 CFStringTokenizer (或者enumerateSubstringsInRange:options:usingBlock:)吧

从字符串解析数据

 

NSScanner 是个用以解析任意或半结构化的字符串的数据的类。当你为一个字符串创建一个扫描器时,你可以指定忽略哪些字符,这样可以避免那些字符以各种各样的方式被包含到解析出来的结果中。

 

例如,你想从这样一个字符串中解析出开门时间:

 

Mon-Thurs: 8:00 - 18:00 Fri: 7:00 - 17:00 Sat-Sun: 10:00 - 15:00

 

你会 enumerateLinesUsingBlock: 并像这样用一个 NSScanner 来解析:

 1     NSMutableCharacterSet *skippedCharacters = [NSMutableCharacterSet punctuationCharacterSet];

 2     [skippedCharacters formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];

 3     

 4     [hours enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {

 5         NSScanner *scanner = [NSScanner scannerWithString:line];

 6         [scanner setCharactersToBeSkipped:skippedCharacters];

 7         

 8         NSString *startDay, *endDay;

 9         NSUInteger startHour, startMinute, endHour, endMinute;

10         

11         [scanner scanCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:&startDay];

12         [scanner scanCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:&endDay];

13         

14         [scanner scanInteger:&startHour];

15         [scanner scanInteger:&startMinute];

16         [scanner scanInteger:&endHour];

17         [scanner scanInteger:&endMinute];

18     }];

我们首先从空格字符集和标点符号字符集的并集构造了一个 NSMutableCharacterSet。告诉 NSScanner忽略这些字符以极大地减少解析这些字符的必要逻辑。

scanCharactersFromSet: 传入字母字符集得到每项中一星期内的开始和结束(可选)的天数。scanInteger 类似地,得到下一个连续的整型值。

NSCharacterSet 和 NSScanner 让你可以快速而充满自信地编码。这两者真是完美组合。

 

你可能感兴趣的:(character)