0

I wonder how could I able to find all the numbers as follows.

Input

NSString* input = @ "1m3s"

The desired output

@[@"1" @"3"]

I have tried the following approach

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

it returned me

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

Is there a better approach to solve this problem, any suggestion?

Even though this question has been marked as a duplication, I have tested the following answer but did not work.

Update

That could be any string value.

if input is @"11m32s" or @11m32 and then desired output would be @[@"11", @"32"];

if input is @"11x32" or @11x32y and then desired output would be @[@"11", @"32"];

casillas
  • 16,351
  • 19
  • 115
  • 215

8 Answers8

3

Using a NSScanner would allow you to scan floats as well. In addition your array is populated directly with NSNumber instead of 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);
Stefanf
  • 1,663
  • 13
  • 15
1

Try:

NSString* input = @ "1m3s";
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
NSArray *outArray = [input componentsSeparatedByCharactersInSet:nonDigitCharacterSet];
outArray = [outArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]];
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
1

To extract numbers you can use Regular Expression.

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);

Or if you want to be more specific and extract the numbers in an expression ##m##s use

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);
}
vadian
  • 274,689
  • 30
  • 353
  • 361
0

I think's it's helpful for you below the code work for me.

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);
Ravi Dhorajiya
  • 1,531
  • 3
  • 21
  • 26
  • The use of `%c` will only work for ASCII (8-bit) characters. But `characterAtIndex` returns 16-bit characters. At least use `%C`. – rmaddy Jul 11 '17 at 03:54
0

Use below code :

 NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
 NSArray *outArray = [[input componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];
Vinit Ingale
  • 381
  • 1
  • 4
  • 17
0

Use this handy approach:

+ (NSArray *)extractNumberFromText:(NSString *)text
{
    NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    NSMutableArray *numberArray = [[text componentsSeparatedByCharactersInSet:nonDigitCharacterSet]mutableCopy];
    [numberArray removeObject:@""];
    return numberArray;
}
nayem
  • 7,285
  • 1
  • 33
  • 51
  • That also returns me `@[@"1",@"3",@"@"]` – casillas Jul 11 '17 at 04:02
  • Nope. If you have non digit character at the last then you will get an `empty` string. Removing the empty one from the array will get you the numbers only. – nayem Jul 11 '17 at 04:09
0

Here is code to get your desire output:

  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);
Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56
0

You can also use this code bellow.

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