christo16
christo16

Reputation: 4843

Remove double quotes from NSString

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?

Upvotes: 4

Views: 14135

Answers (6)

Yasuhiro Takagi
Yasuhiro Takagi

Reputation: 41

How about this ?

NSCharacterSet *quoteCharset = [NSCharacterSet characterSetWithCharactersInString:@"\""];
NSString *trimmedString = [toBeTrimmedString stringByTrimmingCharactersInSet:quoteCharset];

Upvotes: 4

Hossam Ghareeb
Hossam Ghareeb

Reputation: 7113

You can use

string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [string length])];

Upvotes: 0

zneak
zneak

Reputation: 138041

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?

Upvotes: 0

user120587
user120587

Reputation:

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.

Upvotes: 2

mipadi
mipadi

Reputation: 410602

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

[mString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mString length])];

Upvotes: 17

NSResponder
NSResponder

Reputation: 16861

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

Upvotes: 0

Related Questions