Laur Stefan
Laur Stefan

Reputation: 1579

Replace simultaneously multiple char in NSString

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);

Upvotes: 0

Views: 119

Answers (1)

savner
savner

Reputation: 840

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:@"_"];

Upvotes: 2

Related Questions