Replacing multiple characters in NSString by multiple other characters using a dictionary

房东的猫 提交于 2019-11-28 05:51:59

问题


I would like to replace some characters in my string, by other characters, using a dictionary.

For instance, every "a" should be replaced by "1", and every "1" should be replaced by "9". What I don't want is every "a" to be replaced twice, ending up with a "9". Every character must be replaced just once.

I got this working using the following code, but I feel like it can be done more efficient. Is this really the best I can do, or can you help me improve my code?

NSDictionary *replacements = [NSDictionary dictionaryWithObjectsAndKeys:
                              // Object, Key,
                              @"1", @"a",
                              @"2", @"b",
                              @"3", @"c",
                              @"9", @"1",
                              @"8", @"2",
                              @"7", @"3",
                              nil];

NSString *string = @"abc-123";
NSMutableString *newString = [NSMutableString stringWithCapacity:0];

for (NSInteger i = 0; i < string.length; i++)
{
    NSString *c = [NSString stringWithFormat:@"%C", [string characterAtIndex:i]];
    id replacement = [replacements objectForKey:c];
    if (replacement != nil) {
        [newString appendString:replacement];
    } else {
        [newString appendString:c];
    }
}

NSLog(@"newString: %@", newString); // newString: 123-987 (Works!)

Just to be clear: This code is working for me, I just feel like it's very inefficient. I'm Looking for ways to improve it.

Thank you.


回答1:


The following code is perhaps not much faster, but slightly simpler and shorter. It enumerates all characters of the string with a method that works correctly even with composed characters such as Emojis (which are stored as two characters in the string).

NSMutableString *newString = [string mutableCopy];

[newString enumerateSubstringsInRange:NSMakeRange(0, [newString length])
                  options:NSStringEnumerationByComposedCharacterSequences
               usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
       NSString *repl = replacements[substring];
       if (repl != nil) {
           [newString replaceCharactersInRange:substringRange withString:repl];
       }
}];



回答2:


First Replace Every "1" by "9" and then Replace Every "a" by "1". Whats wrong with the logic ?

Your Dictionary is

@"1", @"a",
@"2", @"b",
@"3", @"c",
@"9", @"1",
@"8", @"2",
@"7", @"3",
nil

Replacing "1" by "9" will create

@"9", @"a",
@"2", @"b",
@"3", @"c",
@"9", @"9",
@"8", @"2",
@"7", @"3",
nil

and then Replacing "a" by "1" will create

@"9", @"1",
@"2", @"b",
@"3", @"c",
@"9", @"9",
@"8", @"2",
@"7", @"3",
nil

Do you want this as your Desired result ?




回答3:


If ur rule is static like u said...i dont have sure if it is better

NSString *string = @"abc-123";
NSMutableString *newString = [NSMutableString stringWithCapacity:0];

for (NSInteger i = 0; i < string.length; i++)
{

    unichar cu = [string characterAtIndex:i];
    if (cu >=97 && cu<=99){
        cu -= 48;
    }else if (cu>=49 && cu<=51){
        cu = cu+10-((cu-48)*2);
    }
    [newString appendString:[NSString stringWithFormat:@"%C",cu]];
}

NSLog(@"newString: %@", newString);

I'm using ASCII codes to match ur exactly rule.



来源:https://stackoverflow.com/questions/19313631/replacing-multiple-characters-in-nsstring-by-multiple-other-characters-using-a-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!