替换目标c中的转义字符序列

rso*_*son 2 string objective-c

我有一个返回字符串的查询,以及一个转义字符序列.(例如"美国")

stringByReplacingOccurrencesOfString以这种方式使用:

[theCountry stringByReplacingOccurrencesOfString:@"\"" withString:@""];
Run Code Online (Sandbox Code Playgroud)

但它仍然留下一组报价.如果我再次尝试删除它们的方法:

[theCountry stringByReplacingOccurrencesOfString:@""" withString:@""];
Run Code Online (Sandbox Code Playgroud)

我会有一套不完整的引号......我需要摆脱斜线和双引号.

有什么想法吗?

nal*_*all 7

你需要逃避反斜杠:

NSString* foo = @"USA\\\"";
NSLog(@"String [%@]", foo);
foo = [foo stringByReplacingOccurrencesOfString:@"\\\"" withString:@""];
NSLog(@"String [%@]", foo);
Run Code Online (Sandbox Code Playgroud)

结果是

2009-11-02 09:15:24.403 test[6098:903] String [USA\"]
2009-11-02 09:15:24.406 test[6098:903] String [USA]
Run Code Online (Sandbox Code Playgroud)