objective c - Replace occurrences of two characters with each other in a string -
i have problem need invert 2 characters within string. example, if string "a*b/c" , want replace occurrences of * / , / *. want resulting string "a/b*c".
using method stringbyreplacingoccurrenceofstring:
doesn't work because don't want first round of replacements affect second:
string = @"a*b/c"; [string stringbyreplacingoccurrencesofstring:@"*" withstring:@"/"]; [string stringbyreplacingoccurrencesofstring:@"/" withstring:@"*"];
this results in "a*b*c", not want. know efficient way of accomplishing this?
string = @"a*b/c"; [string stringbyreplacingoccurrencesofstring:@"*" withstring:@"&"]; [string stringbyreplacingoccurrencesofstring:@"/" withstring:@"*"]; [string stringbyreplacingoccurrencesofstring:@"&" withstring:@"/"];
Comments
Post a Comment