views:

728

answers:

1

I have a query that returns a string, as well as an escape character sequence. (ex. "USA\"")

I am using stringByReplacingOccurrencesOfString in this fashion:

[theCountry stringByReplacingOccurrencesOfString:@"\"" withString:@""];

But it is still leaving behind a set of quotes. and if I were to try the method again to remove them:

[theCountry stringByReplacingOccurrencesOfString:@""" withString:@""];

I would have an incomplete set of quotes... I need to get rid of both the slash and the double quotes.

Any thoughts?

+3  A: 

You need to escape the backslash:

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

Results in

2009-11-02 09:15:24.403 test[6098:903] String [USA\"]
2009-11-02 09:15:24.406 test[6098:903] String [USA]
nall