Ios 替换json请求目标c中的双引号

Ios 替换json请求目标c中的双引号,ios,objective-c,Ios,Objective C,卡在下面 我将替换textfield文本中的”(双引号),并在json请求参数中发送该文本,但下面的代码不起作用 NSString * mString = textfield.text; // Input through ipad/iphone/simulator keypad. [mString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0

卡在下面

我将替换textfield文本中的
(双引号),并在json请求参数中发送该文本,但下面的代码不起作用

NSString * mString = textfield.text; // Input through ipad/iphone/simulator keypad.

[mString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mString length])];.
但是如果我硬编码
NSString*mString=@“\”test”
;//通过xcode输入

replaceCurrencesofString:
正在工作

双引号输入编码是否存在问题

为什么ipad/iphone/simulator键盘上的双引号与xcode双引号的工作原理不同


谢谢。

默认情况下,iOS键盘使用“智能”引号…当您打开带引号的字符串部分时,它使用
左双引号
,当您关闭带引号的部分时,它使用
右双引号

所以要去掉这些,你需要替换双引号,左双引号和右双引号

下面是一个简单的例子:

NSString *origString = _myTextField.text;
NSString *strippedString = [origString copy];

NSArray *replaceChars = @[@"”", @"“", @"\""];

for (NSString *c in replaceChars) {
    strippedString = [strippedString
                      stringByReplacingOccurrencesOfString:c
                      withString:@""];
}

NSLog(@"Orig: [%@] / Stripped: [%@]", origString, strippedString);
示例输出:

Orig: [“test”] / Stripped: [test]
你必须仔细看,但你会看到左右双引号


编辑

更简洁的方式:

NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"”“\""];
NSString *strippedString = [origString stringByTrimmingCharactersInSet:set];
NSLog(@"Orig: [%@] / Stripped: [%@]", origString, strippedString);

但是,正如Sulthan所指出的,你可能需要考虑其他语言的标点符号。

你也可以关闭智能引文。注意,每种语言的智能引号都是不同的。如果一个问题值得问,那么花一些时间来格式化它是值得的。