从字符串中提取数字

从字符串中提取数字:

-(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;

}

你可能感兴趣的:(从字符串中提取数字)