Reputation: 697
In objective-c I want to replace the string "AB\(cd"
to "AB\\(cd"
. This is what I have tried
NSMutableString * str = @"AB\(cd";
NSString * replace = @"\\(";
[str stringByReplacingOccurrencesOfString:@"\("
withString:replace];
NSLog(@"%@",str);
Result is "AB(cd"
but I want it as "AB\\\(cd"
Please can somebody help me on this
Upvotes: 2
Views: 8461
Reputation: 9721
You forgot to assign the return value from [NSString stringByReplacingOccurrencesOfString:withString:]
:
NSString *replaced = [str stringByReplacingOccurrencesOfString:@"\\("
withString:@"\\\\("];
However if you want to replace the original str
then you need to re-assign it with a mutable copy as it's an NSMutableString
:
str = [str stringByReplacingOccurrencesOfString:@"\\("
withString:@"\\\\("] mutableCopy];
Upvotes: 2