Andrew
Andrew

Reputation: 3999

How can I remove quotes from an NSString?

I am trying to remove quotes from something like:

"Hello"

so that the string is just:

Hello

Upvotes: 6

Views: 6872

Answers (2)

Rob Norback
Rob Norback

Reputation: 6599

I only wanted to remove the first quote and the last quote, not the quotes within the string so here's what I did:

challengeKey = @"\"I want to \"remove\" the quotes.\"";
challengeKey = [challengeKey substringFromIndex:1];
challengeKey = [challengeKey substringToIndex:[challengeKey length] - 1];

Hope this helps others looking for the same thing. NSLog and you'll get this output:

I want to "remove" the quotes.

Upvotes: 3

Nathan S.
Nathan S.

Reputation: 5388

Check out Apple's docs:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/

You probably want:

stringByReplacingOccurrencesOfString:withString:

Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement

So, something like this should work:

newString = [myString stringByReplacingOccurrencesOfString:@"\"" withString:@""];

Upvotes: 14

Related Questions