NSCharacterSet 使用说明
NSCharacterSet 和 NSMutableCharacterSet 用面向对象的方式来表示一组Unicode字符,它经常与NSString
及NSScanner
组合起来使用,在不同的字符上做过滤、删除或者分割操作。为了给你提供这些字符是哪些字符的直观印象,请看看NSCharacterSet
提供的类方法
alphanumericCharacterSet
capitalizedLetterCharacterSet
controlCharacterSet
decimalDigitCharacterSet
decomposableCharacterSet
illegalCharacterSet
letterCharacterSet
lowercaseLetterCharacterSet
newlineCharacterSet
nonBaseCharacterSet
punctuationCharacterSet
symbolCharacterSet
uppercaseLetterCharacterSet
whitespaceAndNewlineCharacterSet
whitespaceCharacterSet
我们使用下边的方法 打印看看这些方法
1 void dumpCharacterSet( NSString *name ) 2 { 3 unichar idx; 4 NSCharacterSet *cset = [NSCharacterSet performSelector: NSSelectorFromString(name)]; 5 6 printf("Character set (0-127): %s\n7-Bit: ", [name UTF8String]); 7 8 for( idx = 0; idx < 256; idx++ ) 9 { 10 if ( 128 == idx ) { 11 printf( "\n8-Bit: " ); 12 } 13 14 //Returns a Boolean value that indicates whether a given character is in the receiver. 15 if ([cset characterIsMember: idx]) 16 { 17 //判断字符c是否为可打印字符(含空格) 18 if ( isprint(idx) ) { 19 printf( "%c ", idx); 20 } 21 else { 22 printf( "%02x ", idx); 23 } 24 } 25 } 26 printf("\n\n"); 27 }
1 // Reference output... 2 dumpCharacterSet( @"alphanumericCharacterSet" ); 3 dumpCharacterSet( @"controlCharacterSet" ); 4 dumpCharacterSet( @"decimalDigitCharacterSet" ); 5 dumpCharacterSet( @"decomposableCharacterSet" ); 6 dumpCharacterSet( @"illegalCharacterSet" ); 7 dumpCharacterSet( @"letterCharacterSet" ); 8 dumpCharacterSet( @"lowercaseLetterCharacterSet" ); 9 dumpCharacterSet( @"nonBaseCharacterSet" ); 10 dumpCharacterSet( @"punctuationCharacterSet" ); 11 dumpCharacterSet( @"uppercaseLetterCharacterSet" ); 12 dumpCharacterSet( @"whitespaceAndNewlineCharacterSet" ); 13 dumpCharacterSet( @"whitespaceCharacterSet" );
打印结果如下
Character set (0-127): alphanumericCharacterSet 7-Bit: 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 8-Bit: aa b2 b3 b5 b9 ba bc bd be c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d8 d9 da db dc dd de df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f8 f9 fa fb fc fd fe ff Character set (0-127): controlCharacterSet 7-Bit: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 7f 8-Bit: 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f ad Character set (0-127): decimalDigitCharacterSet 7-Bit: 0 1 2 3 4 5 6 7 8 9 8-Bit: Character set (0-127): decomposableCharacterSet 7-Bit: 8-Bit: c0 c1 c2 c3 c4 c5 c7 c8 c9 ca cb cc cd ce cf d1 d2 d3 d4 d5 d6 d9 da db dc dd e0 e1 e2 e3 e4 e5 e7 e8 e9 ea eb ec ed ee ef f1 f2 f3 f4 f5 f6 f9 fa fb fc fd ff Character set (0-127): illegalCharacterSet 7-Bit: 8-Bit: Character set (0-127): letterCharacterSet 7-Bit: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 8-Bit: aa b5 ba c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d8 d9 da db dc dd de df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f8 f9 fa fb fc fd fe ff Character set (0-127): lowercaseLetterCharacterSet 7-Bit: a b c d e f g h i j k l m n o p q r s t u v w x y z 8-Bit: b5 df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f8 f9 fa fb fc fd fe ff Character set (0-127): nonBaseCharacterSet 7-Bit: 8-Bit: Character set (0-127): punctuationCharacterSet 7-Bit: ! " # % & ' ( ) * , - . / : ; ? @ [ \ ] _ { } 8-Bit: a1 a7 ab b6 b7 bb bf Character set (0-127): uppercaseLetterCharacterSet 7-Bit: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 8-Bit: c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d8 d9 da db dc dd de Character set (0-127): whitespaceAndNewlineCharacterSet 7-Bit: 09 0a 0b 0c 0d 8-Bit: 85 a0 Character set (0-127): whitespaceCharacterSet 7-Bit: 09 8-Bit: a0
非常的直观,当我们 使用的时候对照这个打印结果操作就行,
下边再说一下如何过滤的
1 // Set up for reading testString 2 NSString *testString = @"Los Angeles;8.25;0.580561574;1,Tokyo;1.9;0.643872234;1;Honolulu,0;0;0;Toronto;7.9;5.3322;3;"; 3 // Parse CSV with NSScanner 4 NSScanner *myScanner = [NSScanner scannerWithString:testString]; 5 6 NSString *location; 7 float theRevenue; 8 float thePercent; 9 int theRank; 10 11 // Set up data delimiter using semicolon//分号 12 13 NSCharacterSet *CharacterSet; 14 15 //Returns a character set containing the characters in a given string. 16 CharacterSet = [NSCharacterSet characterSetWithCharactersInString:@";,"]; 17 18 // Double check scanner string 19 NSLog (@"Scanner string\n"); 20 21 //Returns the string with which the receiver was created or initialized. 22 NSLog (@"%@",[myScanner string]); 23 24 // scanner loop start 25 while ([myScanner isAtEnd] == NO) { 26 27 if ( [myScanner scanUpToCharactersFromSet:CharacterSet intoString:&location] ) { 28 NSLog (@"%@",location); 29 } 30 31 // Skipping the ; and ,delimiter 32 if([myScanner scanString:@";" intoString:NULL] || [myScanner scanString:@"," intoString:NULL]) 33 ; 34 // Read Revenue data up to ; delimiter and skipping 35 //Scans for a float value, returning a found value by reference. 36 if([myScanner scanFloat:&theRevenue]) 37 NSLog(@"%lf",theRevenue); 38 if([myScanner scanString:@";" intoString:NULL] || [myScanner scanString:@"," intoString:NULL]) 39 ; 40 41 // Read Percentage data up to ; delimiter and skipping 42 if([myScanner scanFloat:&thePercent]) 43 NSLog(@"%lf",thePercent); 44 if([myScanner scanString:@";" intoString:NULL] || [myScanner scanString:@"," intoString:NULL]) 45 ; 46 47 // Read Ranking data up to ; delimiter and skipping 48 if([myScanner scanInt:&theRank]) 49 NSLog(@"%i",theRank); 50 if([myScanner scanString:@";" intoString:NULL] || [myScanner scanString:@"," intoString:NULL]) 51 ; 52 }
打印结果如下
2016-06-12 15:04:37.967 ModelBenchmark[12012:171056] Los Angeles 2016-06-12 15:04:37.967 ModelBenchmark[12012:171056] 8.250000 2016-06-12 15:04:37.967 ModelBenchmark[12012:171056] 0.580562 2016-06-12 15:04:37.968 ModelBenchmark[12012:171056] 1 2016-06-12 15:04:37.968 ModelBenchmark[12012:171056] Tokyo 2016-06-12 15:04:37.968 ModelBenchmark[12012:171056] 1.900000 2016-06-12 15:04:37.968 ModelBenchmark[12012:171056] 0.643872 2016-06-12 15:04:37.968 ModelBenchmark[12012:171056] 1 2016-06-12 15:04:37.968 ModelBenchmark[12012:171056] Honolulu 2016-06-12 15:04:37.968 ModelBenchmark[12012:171056] 0.000000 2016-06-12 15:04:37.968 ModelBenchmark[12012:171056] 0.000000 2016-06-12 15:04:37.968 ModelBenchmark[12012:171056] 0 2016-06-12 15:04:37.968 ModelBenchmark[12012:171056] Toronto 2016-06-12 15:04:37.968 ModelBenchmark[12012:171056] 7.900000 2016-06-12 15:04:37.969 ModelBenchmark[12012:171056] 5.332200 2016-06-12 15:04:37.969 ModelBenchmark[12012:171056] 3
不难看出,对字符串的过滤非常灵活,因此我们应该使用这种方法来过滤字符串。