0

I have NSString with couple strings like this that the 465544664646 is change between them :

data-context-item-title="465544664646"

How i parse the 465544664646 string to a Array ?

Edit

NSRegularExpression* myRegex = [[NSRegularExpression alloc] initWithPattern:@"(?i)(data-context-item-title=\")(.+?)(\")" options:0 error:nil];
[myRegex enumerateMatchesInString:responseString options:0 range:NSMakeRange(0, [responseString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
    NSRange range = [match rangeAtIndex:1];
    NSString *string =[responseString substringWithRange:range];
    NSLog(string);
}];
YosiFZ
  • 7,792
  • 21
  • 114
  • 221

3 Answers3

1

Try this one:

NSString *yourString=@"data-context-item-title=\"465544664646\" data-context-item-title=\"1212121212\"";

NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:yourString];
[scanner scanUpToString:@"\"" intoString:nil];
while(![scanner isAtEnd]) {
    NSString *tempString;
    [scanner scanString:@"\"" intoString:nil];
    if([scanner scanUpToString:@" " intoString:&tempString]) {
        [substrings addObject:tempString];
    }
    [scanner scanUpToString:@"\"" intoString:nil];
}

//NSLog(@"->%@",substrings); //substrings contains all numbers as string.


for (NSString *str in substrings) {
    NSLog(@"->%ld",[str integerValue]); //converted each number to integer value, if you want to store as NSNumber now you can store each of them in array
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

Something like this?

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

You can use

- (NSArray *)componentsSeparatedByString:(NSString *)separator

method like

NSArray *components = [@"data-context-item-title="465544664646" componentsSeparatedByString:@"\""];

Now you should got the string at 2. index

[components objectAtIndex:1]

Now you can create array from that string using the method here NSString to NSArray

Community
  • 1
  • 1
Mert
  • 6,025
  • 3
  • 21
  • 33