在Objective - C中扫描数字

时间:2017-07-11 03:37:14

标签: ios objective-c nsstring

我想知道如何能够找到所有数字如下。

输入

NSString* input = @ "1m3s"

所需的输出

@[@"1" @"3"]

我尝试过以下方法

NSArray *arr = [input componentsSeparatedByCharactersInSet:
                        [NSCharacterSet characterSetWithCharactersInString:@"ms"]];

它让我回来了

@[@"1",@"3",@"@"]

有没有更好的方法来解决这个问题,有什么建议吗?

尽管此问题已被标记为重复,但我已测试了以下answer,但无效。

更新

这可能是任何字符串值。

如果输入为@"11m32s"@11m32,则所需输出为@[@"11", @"32"];

如果输入为@"11x32"@11x32y,则所需输出为@[@"11", @"32"];

8 个答案:

答案 0 :(得分:3)

使用NSScanner也可以扫描浮点数。另外,您的数组直接使用NSNumber而不是NSString填充。

NSString * input = @ "12m32s";
NSScanner * aScanner = [NSScanner scannerWithString:input];
NSInteger aInt;
NSMutableArray * result = [NSMutableArray array];
while (!aScanner.isAtEnd)
{
    if ([aScanner scanInteger:&aInt])
    {
        [result addObject:@(aInt)];
    }
    if (!aScanner.isAtEnd)
    {
        aScanner.scanLocation += 1;
    }
}
NSLog(@"%@",result);

答案 1 :(得分:2)

尝试:

NSString* input = @ "1m3s";
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
NSArray *outArray = [input componentsSeparatedByCharactersInSet:nonDigitCharacterSet];
outArray = [outArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]];

答案 2 :(得分:2)

要提取数字,您可以使用正则表达式。

NSString* input = @ "1m3s";
NSString *pattern = @"\\d+"; // searches for one or more digits
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSArray *matches = [regex matchesInString:input options:0 range:NSMakeRange(0, input.length)];
NSMutableArray *result = [[NSMutableArray alloc] init];
for (NSTextCheckingResult *match in matches) {
    [result addObject: [input substringWithRange:match.range]];
}
NSLog(@"%@", result);

或者,如果您想要更具体,并在表达式##m##s中使用

提取数字
NSString* input = @ "1m3s";
NSString *pattern = @"(\\d+)m(\\d+)s";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSTextCheckingResult *match = [regex firstMatchInString:input options:0 range:NSMakeRange(0, input.length)];
if (match) {
    NSArray *result = @[[input substringWithRange:[match rangeAtIndex:1]], [input substringWithRange:[match rangeAtIndex:2]]];
    NSLog(@"%@", result);
}

答案 3 :(得分:0)

我认为这对我来说对你有帮助。

NSString *originalString = @"1m3s";
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (int i=0; i<[originalString length]; i++) {
        if (isdigit([originalString characterAtIndex:i])) {
            NSMutableString *strippedString = [NSMutableString stringWithCapacity:10];
            [strippedString appendFormat:@"%c",[originalString characterAtIndex:i]];
            [arr addObject:strippedString];
        }
    }
    NSLog(@"%@",arr.description);

答案 4 :(得分:0)

使用以下代码:

SELECT UCASE(RIGHT(DepartureTime,2)) AS [Time of Departure], COUNT(*) AS [PassengerCount]
FROM Flight
WHERE FlightNumber IN


  (SELECT FlightNumber
   FROM Manifest
   WHERE FlightDate BETWEEN #07/01/2016# AND #12/31/2016# AND PassengerNumber IN


      (SELECT PassengerNumber
       FROM Passenger
       WHERE FFNumber IN

        (SELECT FFNumber
         FROM FrequentFlyer
         WHERE PremierStatus = 3)))

GROUP BY DepartureTime;

答案 5 :(得分:0)

使用这种方便的方法:

+ (NSArray *)extractNumberFromText:(NSString *)text
{
    NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    NSMutableArray *numberArray = [[text componentsSeparatedByCharactersInSet:nonDigitCharacterSet]mutableCopy];
    [numberArray removeObject:@""];
    return numberArray;
}

答案 6 :(得分:0)

以下是获得您的愿望输出的代码:

  NSString* input = @"1m3s";

        NSString *newString = [[input componentsSeparatedByCharactersInSet:
                                [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
                               componentsJoinedByString:@""];


        NSMutableArray *array = [NSMutableArray array];
        for (int i = 0; i < [newString length]; i++) {
            NSString *ch = [newString substringWithRange:NSMakeRange(i, 1)];
            [array addObject:ch];
        }


        NSLog(@"%@",array.description);

答案 7 :(得分:0)

您也可以使用此代码。

NSString * String = @"24fsd35rf";
NSArray* Array = [String componentsSeparatedByCharactersInSet:
                              [[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSLog(@"%@",[Array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self <> ''"]]);