Possible Duplicate:
Remove double quotes from NSString
I have a string with double double quotes "" that I need to replace with single double quotes ".
I've tried the following but I still end up with double double quotes
NSString *str = @"This is a \"\"99\"\" string";
[str stringByReplacingOccurrencesOfString: @"\"\"" withString: @"\""];
Initially the string is: This is a ""99"" string
After the stringByReplacingOc开发者_高级运维currencesOfString: This is a ""99"" string.
What am I doing wrong?
It looks like you are expecting str
to contain your modified string. Instead, you should be looking at the return value of the stringByReplacingOccurrencesOfString:withString:
function:
NSString *str = @"This is a \"\"99\"\" string";
NSString *result =[str stringByReplacingOccurrencesOfString: @"\"\"" withString: @"\""];
NSLog(@"before: %@ after: %@", str, result);
NSString objects are immutable. If you want str to be mutable, look into NSMutableString: documentation link
The method you are calling doesn't change the original NSString. It returns a new NSString with the replacements you've asked for.
Save the new string and use that.
精彩评论