0

I am trying to replace multiple character found in a string at once not using stringByReplacingOccurrencesOfString. So if my string is: @"/BKL_UO+C-" I what to change / to _ ; - to +; + to -; _ to /. In my code I have tried to do this by applying for each of the signs that I want to replace first the stringByReplacingOccurrencesOfString and save it as a new string and created an NSArray of chars with them. Compared the differences and save them in another array where I push all the differences from all 4 arrays. Then apply all differences to the initial string.

Initial hash string is: @"lRocUK/Qy+V2P3yDhCd74RvHjCDzlTfrGMolZZE0pcQ" Expected result: @"lRocUK_Qy-V2P3yDhCd74RvHjCDzlTfrGMolZZE0pcQ"

NSMutableArray *originalHashArrayOfCharFromString = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
    [originalHashArrayOfCharFromString addObject:[NSString stringWithFormat:@"%C", [hash characterAtIndex:i]]];
}

//1 change
NSString *backslashRplecedInHashString = hash;
backslashRplecedInHashString = [backslashRplecedInHashString stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
NSMutableArray *hashConvertBackslashToUnderline = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
    [hashConvertBackslashToUnderline addObject:[NSString stringWithFormat:@"%C", [backslashRplecedInHashString characterAtIndex:i]]];
}
//2 change
NSString *minusRplecedInHashString = hash;
minusRplecedInHashString = [minusRplecedInHashString stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
NSMutableArray *hashConvertMinusToPlus = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
    [hashConvertMinusToPlus addObject:[NSString stringWithFormat:@"%C", [minusRplecedInHashString characterAtIndex:i]]];
}

//3 change
NSString *underlineRplecedInHashString = hash;
underlineRplecedInHashString = [underlineRplecedInHashString stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
NSMutableArray *hashConvertUnderlineToBackslash = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
    [hashConvertUnderlineToBackslash addObject:[NSString stringWithFormat:@"%C", [underlineRplecedInHashString characterAtIndex:i]]];
}

//4 change
NSString *plusRplecedInHashString = hash;
plusRplecedInHashString = [plusRplecedInHashString stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
NSMutableArray *hashConvertPlusToMinus = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
    [hashConvertPlusToMinus addObject:[NSString stringWithFormat:@"%C", [plusRplecedInHashString characterAtIndex:i]]];
}

NSMutableArray *tempArrayForKey = [[NSMutableArray alloc] init];
NSMutableArray *tempArrayForValue = [[NSMutableArray alloc] init];
int possitionInTempArray = 0;

//Store all changes of original array in a temp array
//1 replace
for (int countPosssition = 0 ; countPosssition < [hash length]; countPosssition++) {
    if ([originalHashArrayOfCharFromString objectAtIndex:countPosssition] != [hashConvertBackslashToUnderline objectAtIndex:countPosssition]) {
        [tempArrayForValue  addObject:[hashConvertBackslashToUnderline objectAtIndex:countPosssition]];
        [tempArrayForKey  addObject:[NSNumber numberWithInt:countPosssition]];
        possitionInTempArray++;
    }
}
//2 replace
for (int countPosssition = 0 ; countPosssition < [hash length]; countPosssition++) {
    if ([originalHashArrayOfCharFromString objectAtIndex:countPosssition] != [hashConvertMinusToPlus objectAtIndex:countPosssition]) {

        [tempArrayForValue  addObject:[hashConvertMinusToPlus objectAtIndex:countPosssition]];
        [tempArrayForKey  addObject:[NSNumber numberWithInt:countPosssition]];
        possitionInTempArray++;

    }

}
//3 replace
for (int countPosssition = 0 ; countPosssition < [hash length]; countPosssition++) {
    if ([originalHashArrayOfCharFromString objectAtIndex:countPosssition] != [hashConvertUnderlineToBackslash objectAtIndex:countPosssition]) {

        [tempArrayForValue  addObject:[hashConvertUnderlineToBackslash objectAtIndex:countPosssition]];
        [tempArrayForKey  addObject:[NSNumber numberWithInt:countPosssition]];
        possitionInTempArray++;
    }
}
//4 replace
for (int countPosssition = 0 ; countPosssition < [hash length]; countPosssition++) {
    if ([originalHashArrayOfCharFromString objectAtIndex:countPosssition] != [hashConvertPlusToMinus objectAtIndex:countPosssition]) {

        [tempArrayForValue  addObject:[hashConvertPlusToMinus objectAtIndex:countPosssition]];
        [tempArrayForKey  addObject:[NSNumber numberWithInt:countPosssition]];
        possitionInTempArray++;
    }
}
NSLog(tempArrayForKey.debugDescription);
NSLog(tempArrayForValue.debugDescription);
// use the them array to submit changes
for (int count = 0; count < tempArrayForKey.count; count++) {
        [originalHashArrayOfCharFromString setObject:[tempArrayForValue objectAtIndex:count] atIndexedSubscript:(int)[tempArrayForKey objectAtIndex:count]];
}
[hash setString:@""];
//Reassemble the hash string using his original array that sufferet modificvations
for (int count = 0; count<originalHashArrayOfCharFromString.count; count++) {
    [hash appendString:[NSString stringWithFormat:@"%@", [originalHashArrayOfCharFromString objectAtIndex:count]]];
}


NSLog(hash);
2
  • Any reason on why you don't want to use -replaceOccurrenciesOfString:withString: ? Commented Feb 4, 2015 at 16:25
  • if i have string like: @"aba" and first I convert a -> b Than the result will be @"bbb" and after that I apply the change of b->a the final result will be @"aaa" and I am trying to obtain @"bab". Of course this is a small example the actual string is generated random and is much longer. Commented Feb 4, 2015 at 16:33

1 Answer 1

2

What if you temporarily swapped out the occurrences to a "temp" character so you wouldn't lose data on the swaps and do something like this:

NSString * initialString = @"lRocUK/Qy+V2P3yDhCd74RvHjCDzlTfrGMolZZE0pcQ";
    initialString =[initialString stringByReplacingOccurrencesOfString:@"/" withString:@"^"];
    initialString =[initialString stringByReplacingOccurrencesOfString:@"-" withString:@"*"];
    initialString =[initialString stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
    initialString =[initialString stringByReplacingOccurrencesOfString:@"*" withString:@"+"];
    initialString =[initialString stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
    initialString =[initialString stringByReplacingOccurrencesOfString:@"^" withString:@"_"];
Sign up to request clarification or add additional context in comments.

2 Comments

vote up from me for the suggestion but I was thinking to make something more universal, maybe convert the code into a method but before that I was wanted to know what I am doing wrong :D
Yeah, this is an old programmer's trick. The other option is to simply iterate through the string and do the obvious substitutions in "normal" code. Probably more efficient than doing several stringBy... operations in a row, and likely easier to understand/get right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.