ios Objective-C 中的转义引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10117031/
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-08-30 17:49:04  来源:igfitidea点击:

Escape Quotes in Objective-C

objective-ciosencodingnsstringescaping

提问by Moe

I'm using this code snippet to encode characters to be friendly with a POST request:

我正在使用此代码片段对字符进行编码以对 POST 请求友好:

   NSString *unescaped = [textField text];
   NSString *escapedString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                                  NULL,
                                                                                  (__bridge_retained CFStringRef)unescaped,
                                                                                  NULL,
                                                                                  (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                  kCFStringEncodingUTF8);

Which works great, but does not add escape Quotation marks: "

效果很好,但不添加转义引号: "

How do I escape Quotation marks in IOS?

如何在 IOS 中转义引号?

回答by Moe

I think I didn't word the question correctly.

我想我没有正确表达这个问题。

I needed to take the user inputted NSString from [textField text]and make sure that if there are quotation marks in the string, they are escaped properly in order to send through a POST statement.

我需要从用户输入的 NSString 中获取[textField text]并确保如果字符串中有引号,它们会被正确转义以便通过 POST 语句发送。

My solution was:

我的解决方案是:

unescaped = [unescaped stringByReplacingOccurrencesOfString:@"\"" withString:@"\\""];

Thanks

谢谢

回答by Ken Thomases

First, you don't want to use __bridge_retainedin your cast to a CFStringRef. Just use __bridge.

首先,您不想__bridge_retained在您的演员表中使用CFStringRef. 只需使用__bridge.

Second, you don't have to escape the quotes manually by string replacement. Just add the quote character to the set of characters to be quoted when calling CFURLCreateStringByAddingPercentEscapes(). Like so:

其次,您不必通过字符串替换手动转义引号。只需将引号字符添加到调用CFURLCreateStringByAddingPercentEscapes(). 像这样:

NSString *unescaped = [textField text];
NSString *escapedString = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                    (__bridge CFStringRef)unescaped,
                                                                                    NULL,
                                                                                    CFSTR("!*'();:@&=+$,/?%#[]\""),
                                                                                    kCFStringEncodingUTF8));

(In addition to adding the quote to the set, I changed to use CFBridgingRelease()rather than a __bridge_transfercast because I find it clearer. It satisfies the feeling that all CF "Create" functions need a corresponding "Release". Also, I changed the use of a @""literal cast to CFStringRefto just a CFSTR("")literal.)

(除了把quote加到set里,我改成useCFBridgingRelease()而不是__bridge_transfercast,因为我觉得更清晰。满足了CF所有的“Create”函数都需要一个对应的“Release”的感觉。另外,我把use改成了一个@""字面投来CFStringRef只是一个CFSTR("")文本。)

回答by CodaFi

Quotes are to be escaped with \".

引号是要转义的\"

As in:

如:

(CFStringRef)@"I'm an \"example\""

回答by adi27

try using \" instead of using " directly...

尝试使用 \" 而不是直接使用 "...

Special Characters like Quotes, slashes and others require \ to make that character to remove its special functionality.

引号、斜杠和其他特殊字符需要 \ 才能使该字符删除其特殊功能。