开发者

Replace double double quotes "" with single double quote " [duplicate]

This question already has answers here: Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜