[iOS 取出字串中的數字]

ilmari發表於2018-01-03

//取出字串中的數字

第一種: 字串: 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);
複製程式碼

相關文章