2015-02-04 3 views
0

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

왜 '-replaceOccurrenciesOfString : withString :'을 사용하지 않는지에 대한 이유는 무엇입니까? –

+0

만약 내가 다음과 같은 문자열을 가지고 있다면 : @ "aba"그리고 처음에는 a -> b로 바꾼다. 결과는 @ "bbb"가 될 것이고 그 후에 나는 b -> a의 변화를 적용한다. "aaa" @ "bab"을 얻으려고합니다. 물론 이것은 실제 문자열이 무작위로 생성되고 훨씬 길어지는 작은 예입니다. –

답변

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

제안에서 나에게 표를 던지 겠지만 좀 더 보편적 인 것을 만들려고 생각 했었습니다. 아마도 코드를 메서드로 변환 할 수 있었지만 그 전에는 내가 잘못하고있는 것을 알고 싶었습니다 : D –

+0

예, 이것은 오래된 프로그래머의 트릭입니다. 다른 옵션은 단순히 문자열을 반복하고 "일반"코드에서 명백한 대체를 수행하는 것입니다. 아마도'stringBy ... '연산을 여러 줄로 수행하는 것보다 더 효율적이고 이해하기 쉽고 옳은 것으로 보인다. –