iOS NSScanner的学习使用

NSScanner继承自NSObject,遵守NSCopying协议。是一个用于扫描字符串的抽象类。学会使用这个类,在做对字符串的处理时还是很有帮助的。尤其是在做一些比如将字符串中的某几个关键字变色等。不过这不是本节的关键,因此不做展开。这个类的作用还是要多靠观察和实践,文中有不对的地方多多包涵。


@property (readonly,copy) NSString *string;

// 返回当前被扫描的字符串

@property NSUInteger scanLocation;

// 当前扫描的位置,默认是从0开始的。通过设置这个属性值,可以自定义扫描的位置

@property (nullable,copy) NSCharacterSet *charactersToBeSkipped;

// 这个集合是用来设置我们需要跳过扫描的一个字符集合

@property BOOL caseSensitive;

// 是否大小写敏感

@property (nullable,retain) id locale;

// 具体用法不知

给出文档的解释

A scanner’s locale affects the way it interprets numeric values from the string. In particular, a scanner uses the locale’s decimal separator to distinguish the integer and fractional parts of floating-point representations. A scanner with no locale set uses non-localized values. New scanners have no locale by default.

- (instancetype)initWithString:(NSString *)string;

// 指定的扫描器初始化方法,用给定扫描字符串初始化

- (BOOL)scanInt:(nullableint *)result;

// 判断给定扫描的字符串的int值是不是整型


NSString *scanString = @"123";

int i;

[scanner scanInt:&i]; // 返回YES


- (BOOL)scanInteger:(nullableNSInteger *)result;

// 与上面的方法相同,就是输入的参数类型不同

- (BOOL)scanLongLong:(nullablelong long *)result;

// 扫描长长整型

- (BOOL)scanUnsignedLongLong:(nullableunsigned longlong *)result;

// 扫描无符号的长长整型

- (BOOL)scanFloat:(nullablefloat *)result;

// 扫描float型的数据


NSString *scanString = @"123";

float f;

[scanner scanFloat:&f]; // 返回NO


- (BOOL)scanDouble:(nullabledouble *)result;

// 扫描double类型的数据

- (BOOL)scanHexInt:(nullableunsigned *)result;                                        // Optionally prefixed with "0x" or "0X"

- (BOOL)scanHexLongLong:(nullableunsigned longlong *)result;  // Optionally prefixed with "0x" or "0X"

- (BOOL)scanHexFloat:(nullablefloat *)result;                  // Corresponding to %a or %A formatting. Requires "0x" or "0X" prefix.

- (BOOL)scanHexDouble:(nullabledouble *)result;                // Corresponding to %a or %A formatting. Requires "0x" or "0X" prefix.

// 扫描前缀是否带有0x或者0X



NSString *scanString = @"0x1";

NSScanner *scanner = [NSScanner scannerWithString:scanString];

unsigned r;

[scanner scanHexInt:&r]; // 返回YES


- (BOOL)scanString:(NSString *)string intoString:(NSString *__nullable * __nullable)result;

// 给定一个字符串,从扫描的字符串中找出相同的



NSString *scanString = @"897岁k";

NSScanner *scanner = [NSScanner scannerWithString:scanString];

NSString *availdStr;

while (!scanner.isAtEnd) {

  scanner.scanLocation++;

NSLog(@"%ld",scanner.scanLocation);

[scanner scanString:@"k"intoString:&availdStr];

if (availdStr) {

  NSLog(@"%@",availdStr);

}

}

输出:

2016-05-24 20:15:42.571 NSScanner[5391:383531] 1

2016-05-24 20:15:42.572 NSScanner[5391:383531] 2

2016-05-24 20:15:42.572 NSScanner[5391:383531] 3

2016-05-24 20:15:42.572 NSScanner[5391:383531] 4

2016-05-24 20:15:42.572 NSScanner[5391:383531] k



- (BOOL)scanCharactersFromSet:(NSCharacterSet *)set intoString:(NSString *__nullable * __nullable)result;

// 扫描出给定字符集合中的字符放入到value中,只要是set集合中有的字符或字符串都会被扫描出来,直到下一个扫描空缺。



NSString *scanString = @"897岁kGs5a--@家 1k12&&*)  l12";

NSScanner *scanner = [NSScanner scannerWithString:scanString];

NSString *availdStr;

while (!scanner.isAtEnd) {

  scanner.scanLocation++;

[scanner scanCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@"kG125"]intoString:&availdStr];

if (availdStr) {

  NSLog(@"%@",availdStr);

}

}

输出:

2016-05-24 20:20:33.180 NSScanner[5435:388398] kG

2016-05-24 20:20:33.181 NSScanner[5435:388398] 5

2016-05-24 20:20:33.181 NSScanner[5435:388398] 5

2016-05-24 20:20:33.181 NSScanner[5435:388398] 5

2016-05-24 20:20:33.181 NSScanner[5435:388398] 5

2016-05-24 20:20:33.182 NSScanner[5435:388398] 5

2016-05-24 20:20:33.182 NSScanner[5435:388398] 1k12

2016-05-24 20:20:33.182 NSScanner[5435:388398] 1k12

2016-05-24 20:20:33.182 NSScanner[5435:388398] 1k12

2016-05-24 20:20:33.182 NSScanner[5435:388398] 1k12

2016-05-24 20:20:33.182 NSScanner[5435:388398] 1k12

2016-05-24 20:20:33.183 NSScanner[5435:388398] 1k12

2016-05-24 20:20:33.183 NSScanner[5435:388398] 1k12

2016-05-24 20:20:33.183 NSScanner[5435:388398] 12



- (BOOL)scanUpToString:(NSString *)string intoString:(NSString *__nullable * __nullable)result;

// 扫描到给定的字符串后,将从一开始扫描位开始的地方开始截取,并且去掉指定的string(第一个参数);或者是到扫描结束,将字符串末尾往前直到碰到--为止的字符串截取下来。


NSString *scanString = @"897岁kGs5a--@家--1k12&&*)  l12";

NSScanner *scanner = [NSScanner scannerWithString:scanString];

scanner.scanLocation = 3;

NSString *availdStr;

while (!scanner.isAtEnd) {

    scanner.scanLocation++;

[scanner scanUpToString:@"-"intoString:&availdStr];

if (availdStr) {

  NSLog(@"%@",availdStr);

}

}

输出:

2016-05-24 20:33:04.318 NSScanner[5600:402085] kGs5a

2016-05-24 20:33:04.319 NSScanner[5600:402085] kGs5a

2016-05-24 20:33:04.320 NSScanner[5600:402085] @家

2016-05-24 20:33:04.320 NSScanner[5600:402085] @家

2016-05-24 20:33:04.321 NSScanner[5600:402085] 1k12&&*)  l12


- (BOOL)scanUpToCharactersFromSet:(NSCharacterSet *)set intoString:(NSString *__nullable * __nullable)result;

// 扫到给定集合,原理与上一条相同

+ (instancetype)scannerWithString:(NSString *)string;

// 类方法创建

+ (id)localizedScannerWithString:(NSString *)string;

// 不是很明白

给出文档的解释

Returns an NSScanner object that scans a given string according to the user’s default locale.
An NSScanner object that scans aString according to the user’s default locale.

你可能感兴趣的:(iOS NSScanner的学习使用)