NSScanner
NSScanner:该类主要实现对字符串扫描。并且该扫描必须从头到尾扫描(也可以跳到指定的地方进行扫描),开始扫描必须应用到函数,连续的数字之间可以用空格隔开,如:35 15.2 451,此时可以连续获得3个数据,而不用再去扫描空格。
是否扫描空格:
注意:在扫描的时候,如果 空格是不需要扫描的,那么将会忽略空格。
如下代码:
1 NSString *string = @"my age is d 23 34.0";
2 int i;
3 float f;
4 NSString *theString = @"my age is";
5 NSString *resultString;
6 NSString *dString;
7
8 //扫描字符串
9 NSScanner *theScanner = [NSScanner scannerWithString:string ];
10 [theScanner scanString:theString intoString:&resultString];
11 NSLog(@"resultString is %@",resultString);
12
13 //扫描字符串中的d字符
14 [theScanner scanString:@"d" intoString:&dString];
15 NSLog(@"d string is %@",dString);
16
17
18 //扫描字符串中的整型数据
19 [theScanner scanInteger:&i];
20 NSLog(@"intergent is %d",i);
21 //扫描字符串中的浮点型数据
22 [theScanner scanFloat:&f];
2 int i;
3 float f;
4 NSString *theString = @"my age is";
5 NSString *resultString;
6 NSString *dString;
7
8 //扫描字符串
9 NSScanner *theScanner = [NSScanner scannerWithString:string ];
10 [theScanner scanString:theString intoString:&resultString];
11 NSLog(@"resultString is %@",resultString);
12
13 //扫描字符串中的d字符
14 [theScanner scanString:@"d" intoString:&dString];
15 NSLog(@"d string is %@",dString);
16
17
18 //扫描字符串中的整型数据
19 [theScanner scanInteger:&i];
20 NSLog(@"intergent is %d",i);
21 //扫描字符串中的浮点型数据
22 [theScanner scanFloat:&f];
23 NSLog(@"float is %f",f);
输出的结果为:
2012-08-18 16:01:26.419 ScannerDemo[1225:f803] resultString is my age is
2012-08-18 16:01:29.360 ScannerDemo[1225:f803] d string is d
2012-08-18 16:01:33.735 ScannerDemo[1225:f803] intergent is 23
2012-08-18 16:01:35.359 ScannerDemo[1225:f803] float is 34.000000
2012-08-18 16:01:33.735 ScannerDemo[1225:f803] intergent is 23
2012-08-18 16:01:35.359 ScannerDemo[1225:f803] float is 34.000000
解析:在字符串
string 中,“is”和“d”之间的空格不需要扫描的,然后我定义了一个theString 字符串,该字符串之后没有空格,然后扫描 theString d,此时空格没有计入扫描的范围内,所以扫描会忽略空格。
一、将十六进制转化为十进制。如下:
unsigned int r;
NSString *rString = @"EF";