iosobjective-cnsstringnsarraynsmutablestring

Find difference between two comma separated NSMutableString


Suppose I have two NSMutableString like this:-

String 1 ----- {aaa,bss,cdd,dff,eee,fgh}

String 2 ----- {aaa,bss,cdd}

How can we find the the difference between String 1 & String 2 in an NSArray:-

Like this:- { dff,eee,fgh }

As mentioned in duplicate question it is different.


Solution

  • Put both these strings in two different NSMutableSets and then subtract 2nd from 1st. You will have your result.

    NSString* str1 = @"aaa,bss,cdd,dff,eee,fgh";
    NSString* str2 = @"aaa,bss,cdd";
    
    NSMutableSet *set1 = [NSMutableSet setWithArray:[str1 componentsSeparatedByString:@","]];
    NSMutableSet *set2 = [NSMutableSet setWithArray:[str2 componentsSeparatedByString:@","]];
    
    [set1 minusSet:set2];
    
    NSLog(@"result %@",[set1 allObjects]);