stringByReplacingOccurrencesOfString
을 사용하지 않고 문자열에서 여러 문자를 한 번에 바꾸려고합니다. 그래서 만약 내 문자열이 : @"/BKL_UO+C-"
나는 무엇을/to _ to 변경; - ~ +; + to -; _ ~ /. 내 코드에서 나는 먼저 stringByReplacingOccurrencesOfString을 대체하고 새로운 문자열로 저장하고 그것들과 함께 문자의 NSArray를 생성하고자하는 각 표지판을 적용하여이 작업을 시도했습니다. 차이점을 비교하고 다른 어레이에 저장합니다. 여기서 모든 차이점을 모든 4 개의 어레이에서 푸십시오. 그런 다음 모든 차이점을 초기 문자열에 적용하십시오.NSString에서 동시에 여러 문자 바꾸기
초기 해시 문자열 : @"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);
왜 '-replaceOccurrenciesOfString : withString :'을 사용하지 않는지에 대한 이유는 무엇입니까? –
만약 내가 다음과 같은 문자열을 가지고 있다면 : @ "aba"그리고 처음에는 a -> b로 바꾼다. 결과는 @ "bbb"가 될 것이고 그 후에 나는 b -> a의 변화를 적용한다. "aaa" @ "bab"을 얻으려고합니다. 물론 이것은 실제 문자열이 무작위로 생성되고 훨씬 길어지는 작은 예입니다. –