2010-11-08 106 views

回答

13

退房苹果的文档:

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

您可能想要:

stringByReplacingOccurrencesOfString:withString: 

返回一个新字符串,其中接收方中所有出现的目标字符串被另一个给定的字符串替换。

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

所以,这样的事情应该工作:

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

最后一个例子中,你缺少'withString'。 – 2010-11-08 05:33:57

+0

谢谢;接得好。 (固定)。 – 2010-11-08 05:35:38

+3

stringByReplacing也会将“Hell”改为Hello,这不是所需的行为。应该使用stringByTrimmingCharactersInSet – 2010-11-08 18:35:42

4

我只是想删除第一个报价和最后报价,而不是字符串,所以这里内引号是我所做的:

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

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

I want to "remove" the quotes. 
+0

你有一个错误,它应该是[challengeKey length] - 1,不是2 – malhal 2015-07-21 00:13:57

+0

改变了它,你会介意upvoting吗? – 2015-07-22 23:41:33

+1

它现在的作品谢谢,upvoted – malhal 2015-07-24 01:03:23