简体   繁体   中英

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

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:@""];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM