iOS 零散知识点第一篇

1.比正则表达式好用的NSScanner

NSString *originalStr = @"187-6603*2469&";
NSMutableString *stripStr = [NSMutableString stringWithCapacity:originalStr.length];
NSScanner *scanner = [NSScanner scannerWithString:originalStr];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

while ([scanner isAtEnd] == NO) {
    NSString *buffer;
    if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
        [stripStr appendString:buffer];
        NSLog(@"subString = %@", buffer);
    } else {
        NSLog(@"canLocation = %@", @([scanner scanLocation]));
        [scanner setScanLocation:[scanner scanLocation] + 1];
    }
}

2019-02-11 11:23:16.549233+0800 ScannerTest[15577:189256] subString = 187
2019-02-11 11:23:16.549379+0800 ScannerTest[15577:189256] canLocation = 3
2019-02-11 11:23:16.549447+0800 ScannerTest[15577:189256] subString = 6603
2019-02-11 11:23:16.549525+0800 ScannerTest[15577:189256] canLocation = 8
2019-02-11 11:23:16.549598+0800 ScannerTest[15577:189256] subString = 2469
2019-02-11 11:29:38.176873+0800 ScannerTest[15629:198095] canLocation = 13

你可能感兴趣的:(iOS 零散知识点第一篇)