在NSString中同时替换多个char

时间:2015-02-04 16:19:33

标签: ios objective-c string

我试图在不使用stringByReplacingOccurrencesOfString的情况下立即替换字符串中的多个字符。所以如果我的字符串是:@"/BKL_UO+C-"我要改变什么/到_; - 到+; +到 - ; _ 至 /。 在我的代码中,我试图通过申请我想要首先替换stringByReplacingOccurrencesOfString的每个符号并将其保存为新字符串并使用它们创建一个字符的NSArray来尝试这样做。比较差异并将它们保存在另一个阵列中,其中我推动了所有4个阵列的所有差异。然后将所有差异应用于初始字符串。

初始哈希字符串是:@"lRocUK/Qy+V2P3yDhCd74RvHjCDzlTfrGMolZZE0pcQ" 预期结果:@"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);

1 个答案:

答案 0 :(得分:2)

如果你暂时将事件换成“临时”字符会怎么样,这样你就不会丢失交换数据并执行以下操作:

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