【问题标题】:How to find similar strings in array and rename the strings?如何在数组中找到相似的字符串并重命名字符串?
【发布时间】:2011-06-09 05:18:21
【问题描述】:

我想在数组/字典中找到类似的字符串,并通过在字符串中添加后缀来重命名它们。例如假设我的数组/字典包含这样的字符串:

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

等等。可能有任何类似的字符串。现在我想像这样重命名它们:

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

【问题讨论】:

    标签: nsstring nsarray nsdictionary batch-rename


    【解决方案1】:

    好吧,我不知道字典,但是对于数组,你可以这样做。

        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]];
    
        }
        }
    

    祝你好运

    【讨论】:

    • 没有修复字符串。可能有任何字符串女巫与另一个相同
    • 好的,然后运行一个初步函数,获取所有不同的字符串,然后执行类似于上面的操作
    猜你喜欢
    • 2021-05-26
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    相关资源
    最近更新 更多