I have a long string like this :
je!tress:e?tu!tress:es?il!tress:e?nous!tress:ons?vous!tress:ez?ils!tress:ent?
i want to split the string after : and before ? . So this i want to have into array :
{e,es,e,ons,ez,en}
i know that i can do this:
 NSArray *subStrings = [stringToChange componentsSeparatedByString:@":"];
but it's not enough. How can i do this ,please? Any help will be appreciate !
Another option could be to use NSRegularExpression class.
Using this pattern:
\\:([^\?]+)\?
Sample code:
NSString *sample  = @"je!tress:e?tu!tress:es?il!tress:e?nous!tress:ons?vous!tress:ez?ils!tress:ent?";
NSString *pattern = @"\\:([^\?]+)\?";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];
NSArray *matches = [regex matchesInString:sample
                                  options:0
                                    range:NSMakeRange(0, [sample length])];
if(!error && [matches count] > 0) {
    for (NSTextCheckingResult *match in matches) {
        NSRange matchRange = [match range];
        NSLog(@"%@" ,[sample substringWithRange:matchRange]);
    }
}
Output:
2015-07-03 12:16:39.037 TestRegex[3479:48301] :e
2015-07-03 12:16:39.039 TestRegex[3479:48301] :es
2015-07-03 12:16:39.039 TestRegex[3479:48301] :e
2015-07-03 12:16:39.040 TestRegex[3479:48301] :ons
2015-07-03 12:16:39.040 TestRegex[3479:48301] :ez
2015-07-03 12:16:39.040 TestRegex[3479:48301] :ent
                        Use NSScanner to parse through the string, searching for : and ? tags. You can discard the text you don't want and capture the parts you do (based on which tag you find) and add them to an array.
try this,
  NSString *names = @"je!tress:e?tu!tress:es?il!tress:e?nous!tress:ons?vous!tress:ez?ils!tress:ent?";
NSArray *namesarray = [names componentsSeparatedByString:@":"];
NSMutableArray *desiredArray = [[NSMutableArray alloc] initWithCapacity:0];
[namesarray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSRange rangeofsign = [(NSString*)obj rangeOfString:@"?"];
NSString *extractedName = [(NSString*)obj substringToIndex:rangeofsign.location];
[desiredArray addObject:extractedName];
 }];
NSLog(@"%@",desiredArray);
                        To be simpler, you could try the following code:
NSString *string = @"je!tress:e?tu!tress:es?il!tress:e?nous!tress:ons?vous!tress:ez?ils!tress:ent?";
NSArray *firstArray = [string componentsSeparatedByString:@":"];
NSMutableArray *finalArray = [[NSMutableArray alloc] init];
for (NSString *newString in firstArray) {
    NSArray *tempArray = [newString componentsSeparatedByString:@"?"];
    if ([tempArray count] > 1) {
        [finalArray addObject:[tempArray firstObject]];
    }
}
NSLog(@"%@",finalArray);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With