Search code examples
iossortingnsstringnsscanner

NSString remove duplicated substrings


How can I remove duplicated substings from string? For ex. I have: aaa,bbb,ttt,bbb,rrr. And in result I want to have aaa,bbb,ttt,rrr (deleted duplicated bbb). I hope for your help. Thanks.


Solution

  • Do it in three steps

    1) NSArray *items = [theString componentsSeparatedByString:@","];

    2) remove duplicate element from array

        NSArray* array =  [NSArray arrayWithObjects:@"test1", @"test2", @"test1", @"test2",@"test4",   nil];
    
        NSArray* filteredArray = [[NSArray alloc] init];
        NSSet *set= [NSOrderedSet orderedSetWithArray:array];
        filteredArray = [set allObjects];
    

    3) Concate String from array

    NSString *myString = [myArray componentsJoinedByString:@","];