views:

370

answers:

4

How do I remove double quotes from an NSString. Example:

//theMutableString: "Hello World"

[theMutableString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:(NSRange){0,[theMutableString length]}]

It doesn't seem to work, maybe since I had to escape the " in the replaceOccurrencesOfString?

A: 

What happens if you use NSMakeRange(0, [theMutableString length]) instead of trying to cast an inline struct?

NSResponder
+2  A: 

Use the NSMakeRange function instead of your cast. This'll work:

[mString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mString length])];
mipadi
It also works without the NSMakeRange function.
Brandon Bodnár
Works thanks! I could have sworn I tried that.
christo16
Yeah, I can confirm that. It works without the NSMakeRange (though I encourage the use of NSMakeRange).
zneak
Yeah, I don't think that's the source of the problem, since it works either way for me, too (although use of `NSMakeRange` is probably better).
mipadi
+1  A: 

Assuming the mString bit is a typo. I ran this code and the answer was as expected

NSMutableString * theMutableString = [[NSMutableString alloc] initWithString:@"\"Hello World!\""];
NSLog(@"%@",theMutableString);

[theMutableString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:(NSRange){0,[theMutableString length]}];

NSLog(@"%@",theMutableString);

[theMutableString release];

Output

2010-01-23 15:49:42.880 Stringtest[2039:a0f] "Hello World!"
2010-01-23 15:49:42.883 Stringtest[2039:a0f] Hello World!

So it mString was a typo in your question, then your code is correct and the problem is elsewhere. If mString is a typo in your code than that would be the issue.

Brandon Bodnár
A: 

I works perfectly on my end (I copy-pasted your replace line), with both NSMakeRange and the inline struct cast. Are you sure the quotes in your NSString are really the ASCII character 0x22?

zneak