我有一个包含单词和数字的字符串。如何从字符串中提取该数字?
NSString *str = @"This is my string. #1234";
我希望能够将1234剥离为int。每次搜索时,字符串都会有不同的数字和单词。
想法?
答案 0 :(得分:100)
以下是基于NSScanner的解决方案:
// Input
NSString *originalString = @"This is my string. #1234";
// Intermediate
NSString *numberString;
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
// Throw away characters before the first number.
[scanner scanUpToCharactersFromSet:numbers intoString:NULL];
// Collect numbers.
[scanner scanCharactersFromSet:numbers intoString:&numberString];
// Result.
int number = [numberString integerValue];
(其中一些)假设在这里:
int
。或者,您可以直接扫描到int
:
[scanner scanUpToCharactersFromSet:numbers intoString:NULL];
int number;
[scanner scanInt:&number];
如果#
标记字符串中数字的开头,您可以通过以下方式找到它:
[scanner scanUpToString:@"#" intoString:NULL];
[scanner setScanLocation:[scanner scanLocation] + 1];
// Now scan for int as before.
答案 1 :(得分:43)
自包含解决方案:
+ (NSString *)extractNumberFromText:(NSString *)text
{
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
return [[text componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];
}
处理以下案件:
希望这有帮助!
答案 2 :(得分:4)
您可以使用自iOS SDK 4以来可用的NSRegularExpression类。
用一个简单的代码提取整数(“\ d +”正则表达式模式):
- (NSArray*) getIntNumbersFromString: (NSString*) string {
NSMutableArray* numberArray = [NSMutableArray new];
NSString* regexPattern = @"\\d+";
NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:regexPattern options:0 error:nil];
NSArray* matches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
for( NSTextCheckingResult* match in matches) {
NSString* strNumber = [string substringWithRange:match.range];
[numberArray addObject:[NSNumber numberWithInt:strNumber.intValue]];
}
return numberArray;
}
答案 3 :(得分:3)
尝试使用Stack Overflow中的this answer获取一个可以解决这个问题的优秀C代码:
for (int i=0; i<[str length]; i++) {
if (isdigit([str characterAtIndex:i])) {
[strippedString appendFormat:@"%c",[str characterAtIndex:i]];
}
}
答案 4 :(得分:1)
到目前为止最好的解决方案!我认为regexp会更好,但我有点sux它;-)这会过滤所有数字并将它们连接在一起,形成一个新的字符串。如果要拆分多个数字,请稍微更改一下。请记住,当你在一个大循环中使用它时,它会降低性能!
NSString *str= @"bla bla bla #123 bla bla 789";
NSMutableString *newStr = [[NSMutableString alloc] init];;
int j = [str length];
for (int i=0; i<j; i++) {
if ([str characterAtIndex:i] >=48 && [str characterAtIndex:i] <=59) {
[newStr appendFormat:@"%c",[str characterAtIndex:i]];
}
}
NSLog(@"%@ as int:%i", newStr, [newStr intValue]);
答案 5 :(得分:0)
NSPredicate是使用ICU regular expression解析字符串的Cocoa类。
答案 6 :(得分:0)
从字符串中获取数字的Swift扩展
extension NSString {
func getNumFromString() -> String? {
var numberString: NSString?
let thisScanner = NSScanner(string: self as String)
let numbers = NSCharacterSet(charactersInString: "0123456789")
thisScanner.scanUpToCharactersFromSet(numbers, intoString: nil)
thisScanner.scanCharactersFromSet(numbers, intoString: &numberString)
return numberString as? String;
}
}