I have a problem where I need to invert two characters within a string. For example, if my string is "a*b/c" and I want to replace occurrences of * with / and / with *. I want the resulting string to be "a/b*c".
Using the method stringByReplacingOccurrenceOfString:
doesn't work because I don't want the first round of replacements to affect the second:
string = @"a*b/c";
[string stringByReplacingOccurrencesOfString:@"*" withString:@"/"];
[string stringByReplacingOccurrencesOfString:@"/" withString:@"*"];
This results in "a*b*c", which is not what I want. Does anybody know an efficient way of accomplishing this?
string = @"a*b/c";
[string stringByReplacingOccurrencesOfString:@"*" withString:@"&"];
[string stringByReplacingOccurrencesOfString:@"/" withString:@"*"];
[string stringByReplacingOccurrencesOfString:@"&" withString:@"/"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With