开发者

How to find similar strings in array and rename the strings?

开发者 https://www.devze.com 2023-03-11 05:42 出处:网络
I want to find similar strings in an array/dictionary and ren开发者_高级运维ame them by adding a postfix to the string. For example suppose my array/dictionary contains strings like this:

I want to find similar strings in an array/dictionary and ren开发者_高级运维ame them by adding a postfix to the string. For example suppose my array/dictionary contains strings like this:

1.Work
2.Work
3.Work
4.Home
5.Home
6.Mobile
7.Mobile 

etc. there could be any similar strings. Now I want to rename them like this:

1.Work(1)
2.Work(2)
3.Work(3)
4.Home(1)
5.Home(2)
6.Mobile(1)
7.Mobile(2)


Well, i don't know about a dictionary, but for an array you could do something like this.

    NSMutableArray *myArr = [fill the array with w/e];
    int home;
    int mobile;
    int work;
    for(int x = 0; x < [myArr count]; x++){
    NSString *r = [myArr objectAtIndex:(NSUInteger)x];
    if([r isEqualToString:@"work"]){
    work++;
    [myArr replaceObjectAtIndex:x withObject:[NSString strinWithFormat:@"%@(%i)", r, work]];
    }
    else if([r isEqualToString:@"home"]){
    home++;
    [myArr replaceObjectAtIndex:x withObject:[NSString strinWithFormat:@"%@(%i)", r, home]];
    }
    else if([r isEqualToString:@"mobile"]){
    mobile++;

    [myArr replaceObjectAtIndex:x withObject:[NSString strinWithFormat:@"%@(%i)", r, mobile]];

    }
    }

Best of luck

0

精彩评论

暂无评论...
验证码 换一张
取 消