xcode 如何从 NSString 中删除引号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4121478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:54:12  来源:igfitidea点击:

How can I remove quotes from an NSString?

iphoneobjective-cxcode

提问by Andrew

I am trying to remove quotes from something like:

我正在尝试从以下内容中删除引号:

"Hello"

“你好”

so that the string is just:

所以字符串只是:

Hello

你好

回答by Nathan S.

Check out Apple's docs:

查看 Apple 的文档:

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

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

回答by Rob Norback

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:

希望这可以帮助其他人寻找同样的东西。NSLog,你会得到这个输出:

I want to "remove" the quotes.