如何从NSString中删除引号?

时间:2022-08-26 11:10:44

I am trying to remove quotes from something like:

我试图从以下内容中删除引号:

"Hello"

“你好”

so that the string is just:

所以字符串只是:

Hello

你好

2 个解决方案

#1


14  

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

#2


4  

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.

#1


14  

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

#2


4  

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.