Question

I have a string I need to split. It would be easy using componentsSeparatedByString but my problem is that the separator is a comma but I could have commas that aren't separator.

I explain:

My string:

NSString *str = @"black,red, blue,yellow";

the comma between red and blue must not be considered as separator.

I can determine if comma is a separator or not checking if after it there is a white space.

The goal is to obtain an array with:

(
black,
"red, blue",
yellow
)
Was it helpful?

Solution

This is tricky. First replace all occurences of ', ' (comma+space) with say '|' then use components separated method. Once you are done, again replace '|' with ', ' (comma+space).

OTHER TIPS

Just to complete the picture, a solution that uses a regular expression to directly identify commas not followed by white space, as you explain in your question.

As others have suggested, use this pattern to substitute with a temporary separator string and split by that.

NSString *pattern = @",(?!\\s)"; // Match a comma not followed by white space.
NSString *tempSeparator = @"SomeTempSeparatorString"; // You can also just use "|", as long as you are sure it is not in your input.

// Now replace the single commas but not the ones you want to keep
NSString *cleanedStr = [str stringByReplacingOccurrencesOfString: pattern
                                                      withString: tempSeparator
                                                         options: NSRegularExpressionSearch
                                                           range: NSMakeRange(0, str.length)];

// Now all that is needed is to split the string
NSArray *result = [cleanedStr componentsSeparatedByString: tempSeparator];   

If you are not familiar with the regex pattern used, the (?!\\s) is a negative lookahead, which you can find explained quite well, for instance here.

Here is coding implementation for cronyneaus4u's solution:

NSString *str = @"black,red, blue,yellow";
str = [str stringByReplacingOccurrencesOfString:@", " withString:@"|"];
NSArray *wordArray = [str componentsSeparatedByString:@","];
NSMutableArray *finalArray = [NSMutableArray array];
for (NSString *str in wordArray)
{
    str = [str stringByReplacingOccurrencesOfString:@"|" withString:@", "];
    [finalArray addObject:str];
}
NSLog(@"finalArray = %@", finalArray);
NSString *str = @"black,red, blue,yellow";
NSArray *array = [str componentsSeparatedByString:@","];
NSMutableArray *finalArray = [[NSMutableArray alloc] init];
for (int i=0; i < [array count]; i++) {
    NSString *str1 = [array objectAtIndex:i];
    if ([[str1 substringToIndex:1] isEqualToString:@" "]) {
        NSString *str2 = [finalArray objectAtIndex:(i-1)];
        str2 = [NSString stringWithFormat:@"%@,%@",str2,str1];
        [finalArray replaceObjectAtIndex:(i-1) withObject:str2];
    }
    else {
        [finalArray addObject:str1];
    }
}

NSLog(@"final array count : %d description : %@",[finalArray count],[finalArray description]);

Output:

final array count : 3 description : (
black,
"red, blue",
yellow
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top