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 NS_DESIGNATED_INITIALIZER

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


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

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

/*

NSString *scanString = @"123";

int i;

[scanner scanInt:&i]; // 输出值是1

*/


- (BOOL)scanInteger:(nullableNSInteger *)result NS_AVAILABLE(10_5, 2_0);

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


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

// 扫描长长整型


- (BOOL)scanUnsignedLongLong:(nullableunsigned longlong *)result NS_AVAILABLE(10_9,7_0);

// 扫描无符号的长长整型,MAC10.9之后,iOS7.0之后被使用


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

// 扫描float型的数据

/*

NSString *scanString = @"123";

float f;

[scanner scanFloat:&f]; 

*/


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

// 扫描double类型的数据


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

- (BOOL)scanHexLongLong:(nullableunsigned longlong *)result NS_AVAILABLE(10_5,2_0);   // Optionally prefixed with "0x" or "0X"

- (BOOL)scanHexFloat:(nullablefloat *)resultNS_AVAILABLE(10_5,2_0);                  // Corresponding to %a or %A formatting. Requires "0x" or "0X" prefix.

- (BOOL)scanHexDouble:(nullabledouble *)resultNS_AVAILABLE(10_5,2_0);                // Corresponding to %a or %A formatting. Requires "0x" or "0X" prefix.


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

/*

NSString *scanString = @"0x1";

NSScanner *scanner = [NSScannerscannerWithString:scanString];

unsigned r;

[scanner scanHexInt:&r]; // 输出值为1

*/


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

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

/*

    NSString *scanString = @"897k";

    NSScanner *scanner = [NSScannerscannerWithString: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中

/*

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

    NSScanner *scanner = [NSScannerscannerWithString:scanString];    

    NSString *availdStr;

    while (!scanner.isAtEnd) {

        scanner.scanLocation++;

        [scanner scanCharactersFromSet:[NSCharacterSetcharacterSetWithCharactersInString:@"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 =@"897kGs5a--@--1k12&&*)  l12";

    NSScanner *scanner = [NSScannerscannerWithString: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基础到进阶)