1

I have an array, that consist of following strings:

[NSString stringByAppendingFormat:@"<p style=\"padding-left:20px;margin-bottom:-10px;\"><i>%@%@%@%@</i></p>",
                         wrappingBy, pack1, pack2, strFirmName];

For example, it have 200 different strings. First parameter - wrappingBy, may have several different names. For example - box, tube, bag, etc.

What i want is, to enumerate through that array, and create different arrays depending on that name. So, if my array consist of 50 strings start from box, 50 strings start from tube, and 100 strings start from bag i want 3 different arrays.

Is there any easy way to achieve that?

Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • 2
    THe easier way would be storing them into dictionary, and then get the individual key and store them in a separte array! – Teja Nandamuri Aug 11 '16 at 12:57
  • @TejaNandamuri i wonder how could i do that. Each time i get string i presented above is individual case when we get this string by going through SQL db. – Evgeniy Kleban Aug 11 '16 at 12:59
  • Does each array shud hold the name box/bag/tube or the complete string ? – Teja Nandamuri Aug 11 '16 at 13:07
  • @TejaNandamuri complete string. And in each case those box/bag/tube may be names differently. I only know that they be at start and getted out of db by local variable wrappingBy – Evgeniy Kleban Aug 11 '16 at 13:09

1 Answers1

1

Try this:

Let's say you have the array of strings:

NSArray *arr=@[@"box3523sfgsg",@"boxsdfsdf3",@"bag!@$#",@"!@@4bag",@"tube@#$FR",@"tubeASAD"];

In your case, the above array is filled with following string

[NSString stringByAppendingFormat:@"<p style=\"padding-left:20px;margin-bottom:-10px;\"><i>%@%@%@%@</i></p>",
                         wrappingBy, pack1, pack2, strFirmName];

Now add the wrappingBy param to the array everytime you add the above string to the array, and make sure you don;t add duplicates to the array. YOu can check the duplicates before adding them to the array.

and in your case, you would do

        NSMutableArray *arrayNAme=[[NSMutableArray alloc]init];

        //make arrayName mutable
        if (![arrayNAme containsObject: wrappingBy]) {
             [arrayNAme addObject: wrappingBy];
        }

you will get arrayNAMe contain following:

      arrayNAme=@[@"box",@"bag",@"tube"];

Now search the main array string if it contains the wrapingBy names or not, if YES, add them to an array and add that array to the dicitonary:

    NSMutableDictionary *myDictionary=[[NSMutableDictionary alloc]init];
    for( NSString *nameString in arrayNAme) {
       NSMutableArray *strnArray=[[NSMutableArray alloc]init];
       for (NSString *str in arr)
       {
           if([str containsString:nameString])
           {
            [strnArray addObject:str];
            [myDictionary setValue:strnArray forKey:nameString];
           }
       }
    }

At the end you have the dictionary:

    {
    bag =     (
        "bag!@$#",
        "!@@4bag"
    );
    box =     (
        box3523sfgsg,
        boxsdfsdf3
    );
    tube =     (
        "tube@#$FR",
        tubeASAD
    );
}

Now you can get each key value and store them in a separate array.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • thank you, that will save me a lot of time! Nice solution – Evgeniy Kleban Aug 11 '16 at 13:35
  • @EvgeniyKleban try now. LEt me know if it still didn't work for you. – Teja Nandamuri Aug 11 '16 at 15:00
  • sorry but i dont understand clearly how should i modify for.. cycle with your check for duplicate snippet – Evgeniy Kleban Aug 11 '16 at 15:13
  • Did you have [arrayNAme addObject: wrappingBy]; when you got duplicates ? – Teja Nandamuri Aug 11 '16 at 15:14
  • there is no duplicate check in for cycle. Duplicate check is when you add wrappingBy name to the arrayNmae array. – Teja Nandamuri Aug 11 '16 at 15:16
  • i dont understand well, i have my arrayNAme filled with unique strings, i did check. They not dublicate each one. However, block if([str containsString:nameString]) { [strnArray addObject:str]; [myDictionary setValue:strnArray forKey:nameString]; } called 854 times, when there is only 116 items (full strings) – Evgeniy Kleban Aug 11 '16 at 15:17
  • if your arrayNAme contains unique stirngs, then just use the updated for-loop in my answer, you don't have to wrry about duplicate check. – Teja Nandamuri Aug 11 '16 at 15:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120714/discussion-between-teja-nandamuri-and-evgeniy-kleban). – Teja Nandamuri Aug 11 '16 at 15:23