从NSString中删除双引号

时间:2010-01-23 21:40:52

标签: objective-c nsstring

如何从NSString中删除双引号。例如:

//theMutableString: "Hello World"

[theMutableString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:(NSRange){0,[theMutableString length]}]

它似乎不起作用,也许因为我不得不逃避“在replaceOccurrencesOfString?

6 个答案:

答案 0 :(得分:17)

使用NSMakeRange功能代替您的演员。这个工作:

[mString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mString length])];

答案 1 :(得分:4)

这个怎么样?

NSCharacterSet *quoteCharset = [NSCharacterSet characterSetWithCharactersInString:@"\""];
NSString *trimmedString = [toBeTrimmedString stringByTrimmingCharactersInSet:quoteCharset];

答案 2 :(得分:2)

假设mString位是拼写错误。我运行了这段代码,答案是按预期进行的

NSMutableString * theMutableString = [[NSMutableString alloc] initWithString:@"\"Hello World!\""];
NSLog(@"%@",theMutableString);

[theMutableString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:(NSRange){0,[theMutableString length]}];

NSLog(@"%@",theMutableString);

[theMutableString release];

输出

2010-01-23 15:49:42.880 Stringtest[2039:a0f] "Hello World!"
2010-01-23 15:49:42.883 Stringtest[2039:a0f] Hello World!

所以mString在你的问题中是一个错字,那么你的代码是正确的,问题出在其他地方。如果你的代码中的mString是一个拼写错误而不是问题。

答案 3 :(得分:0)

如果使用NSMakeRange(0, [theMutableString length])而不是尝试强制转换内联结构,会发生什么?

答案 4 :(得分:0)

我使用NSMakeRange和内联struct cast完成了我的完美工作(我复制粘贴你的替换行)。你确定你的NSString中的引号实际上是ASCII字符0x22吗?

答案 5 :(得分:0)

您可以使用

string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [string length])];
相关问题