从书面编号中获取int

时间:2014-05-30 18:21:29

标签: ios cocoa-touch string-parsing nsnumberformatter

我需要从写出的数字中获取整数值,例如,在int中从字符串@"three"获取'3'。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

你可以这样做:

NSString * threeString = @"three";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterSpellOutStyle;
formatter.locale = [NSLocale currentLocale];
NSNumber * stringNumb = [formatter numberFromString:threeString];
int threeInt = [stringNumb intValue];
NSLog(@"ThreeInt: %i", threeInt);