从字符串中提取数字

2017-06-22  本文已影响0人  哈布福禄克

从字符串中提取数字:

-(void)findNumFromStr

{

NSString *originalString = @"a1b2c3d4e5f6g7h8i9j0";

NSMutableString *numberString = [[NSMutableString alloc] init];

NSString *tempStr;

NSScanner *scanner = [NSString scannerWithString:originalString];

NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

while(![scanner isAtEnd]){

[scanner scanUpToCharactersFromSet:numbers intoString:NULL];

[scanner scanCharactersFromSet:numbers intoString:&tempStr];

[numberString appendString:tempStr];

tempStr = @"";

}

int number = [numberString integerValue];

return number;

}

上一篇 下一篇

猜你喜欢

热点阅读