如何从NSString中提取两个数字?

时间:2013-05-21 13:06:50

标签: objective-c cocoa nsstring

以下是我的字符串

NSString *myString = @"file_1_23.pdf";
                            ^ ^^

我想要提取的是1& 23。

知道如何获得这个吗?

其他可能的字符串可以是

NSString *myString = @"file_12_2.pdf";
NSString *myString = @"file_123_8.pdf";

我相信我需要使用方法- (NSString *)substringWithRange:(NSRange)range;,但对于如何获得_的第二个索引感到困惑

字符串的格式为

file_anyNumber_anyNumber.fileExtension

注意:它有_两次,这让我很疯狂。

5 个答案:

答案 0 :(得分:3)

这会将字符串的组件分解为字符串数组。

NSString *myString = @"file_123_8.pdf";

NSCharacterSet *delimiters = [NSCharacterSet characterSetWithCharactersInString:@"_."];
NSArray *components = [myString componentsSeparatedByCharactersInSet:delimiters];

现在components数组包含字符串对象@"file", @"123", @"8", @"pdf"

答案 1 :(得分:2)

NSString *myString = @"file_123_8.pdf";

NSArray *subStrings = [myString componentsSeparatedByString:@"_"];

NSString *myFirstString = [subStrings objectAtIndex:1];

NSString *lastString = [subStrings objectAtIndex:2];

NSArray *subStrings = [myString componentsSeparatedByString:@"."];

NSString *mySecondString = [subStrings objectAtIndex:0];

NSLog(@"First String is : %@  ,Second String is : %@", myFirstString, mySecondString);

OUTPUT

First String is : 123  ,Second String is : 8

答案 2 :(得分:0)

希望这会对你有帮助。

-(NSString*)stringBetweenString:(NSString*)start andString:(NSString)end {
    NSRange startRange = [self rangeOfString:start];
    if (startRange.location != NSNotFound) {
        NSRange targetRange;
        targetRange.location = startRange.location + startRange.length;
        targetRange.length = [self length] - targetRange.location;   
        NSRange endRange = [self rangeOfString:end options:0 range:targetRange];
        if (endRange.location != NSNotFound) {
           targetRange.length = endRange.location - targetRange.location;
           return [self substringWithRange:targetRange];
        }
    }
    return nil;
}

答案 3 :(得分:0)

将类别添加到NSString(NSString + Regex)

实施方法

- (NSArray *)matches:(NSString *)pattern
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];

    NSArray *matches = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];

    matches;
}

现在用你的方法做什么

NSArray *matches = [myString matches:@"(\\d)"];

for (NSTextCheckingResult *match in matches) 
{
    NSString *singleMatch = [myString match.range];
    NSLog(@"%@",singleMath);
}

这是未经测试但应该有效。如果您不想创建类别,只需在其他地方添加该方法,添加一个字符串作为参数之一,并用该参数替换self

答案 4 :(得分:0)

这里有几个好的答案。

为了完整性,我们提供了一个基于NSScanner的解决方案,这个类是为这类任务明确制定的(当然不是其他解决方案无效)。

NSString *myString = @"file_1_23.pdf";
NSString *firstString;
NSString *secondString;

NSScanner *scanner = [NSScanner scannerWithString: myString];

NSCharacterSet *digits = [NSCharacterSet decimalDigitCharacterSet];

[scanner scanUpToString: @"_" intoString: NULL];
[scanner scanString: @"_" intoString: NULL];      // Consume the underscore

// Now, read the the first number
[scanner scanUpToString: @"_" intoString: &firstString];

[scanner scanString: @"_" intoString: NULL];      // Consume this underscore, too

// Read the second number
[scanner scanUpToString: @"." intoString: &secondString];

还有其他方法可以扫描字符串,但是这个方法会明确地扫描你提到的两个下划线字符(_)。