Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string after : and before?

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 !

like image 781
Adina Marin Avatar asked Jul 03 '15 06:07

Adina Marin


4 Answers

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
like image 161
NeverHopeless Avatar answered Nov 14 '22 07:11

NeverHopeless


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.

like image 35
Wain Avatar answered Nov 14 '22 07:11

Wain


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);
like image 1
Sheshnath Avatar answered Nov 14 '22 07:11

Sheshnath


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);
like image 1
danialzahid94 Avatar answered Nov 14 '22 08:11

danialzahid94