[iOS---取出字符串中的数字]

//取出字符串中的数字

第一种:
字符串: urlString
NSScanner *scanner = [NSScanner scannerWithString:urlString]; [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil]; int number; [scanner scanInt:&number]; NSString *num=[NSString stringWithFormat:@"%d",number];

第二种:
字符串: urlString

NSCharacterSet* nonDigits =[[NSCharacterSet decimalDigitCharacterSet] invertedSet];
int remainSecond =[[urlString stringByTrimmingCharactersInSet:nonDigits] intValue];
NSLog(@" num %d ",remainSecond);

你可能感兴趣的:([iOS---取出字符串中的数字])