I have the following implementation where a comboKey
is a string that I am trying to retrieve the integers.
For example if the comboKey
is 2m1s
and then it returns @[@"2",@"1"]
; which is perfect.
If comboKey
is 0m2s
and then it returns @[@"0",@"2"];
, however I do not want to have 0
. I only want have positive number(s) @[@"2"];
+ (NSArray*)comboCategoryItems : (NSString*)comboKey
{
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
NSArray *outArray = [comboKey componentsSeparatedByCharactersInSet:nonDigitCharacterSet];
outArray = [outArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]];
return outArray;
}
Here is another way to do this, using regular expressions
NSRegularExpression * exp = [[NSRegularExpression alloc]initWithPattern:@"([1-9]([0-9])*)+" options:NSRegularExpressionDotMatchesLineSeparators error:nil];
NSString * text = @"80m254s";
NSMutableArray *resultArray = [NSMutableArray array];
[exp enumerateMatchesInString:text options:NSMatchingWithoutAnchoringBounds range:NSMakeRange(0, text.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
[resultArray addObject:[text substringWithRange:[result range]]];
}];
NSLog(@"%@",resultArray);
for @"0m2s"
2017-07-15 22:31:18.576397 RegexTestingProject[21823:1942182] ( 2 )
for @"80m254s"
2017-07-15 22:33:50.378485 RegexTestingProject[21826:1942829] ( 80, 254 )
for @"080m254s"
2017-07-15 22:35:40.760626 RegexTestingProject[21828:1943403] ( 80, 254 )
Hope this helps you