【问题标题】:How to escape double quotes in string?如何转义字符串中的双引号?
【发布时间】:2010-11-24 01:14:15
【问题描述】:

我希望双引号出现在以下字符串中,这样它看起来像:

"hi there == "

这是我正在使用的代码:

NSMutableString *s = [[NSMutableString alloc] init];
[s appendString:@""""];
[s appendString:@"hi there == ""\n\r"];

相反,我只得到:

hi there ==

有什么想法吗?

【问题讨论】:

  • 需要注意的是[s appendString:@"hi there == ""\n\r"];无意中利用了 C/C++/ObjC 特性:将两个彼此相邻放置的字符串文字(中间有或没有空格)连接起来。

标签: objective-c cocoa


【解决方案1】:
[s appendString:@"hi there == \"\n\r"];

\"" 所需要的 - 这是标准 C 格式。

【讨论】:

    【解决方案2】:

    虽然晚了,但我们可以试试这个:

    [NSString stringWithFormat:@"\"Hi There=\" Other text"];
    

    【讨论】:

      【解决方案3】:

      您必须在引号 (") 之前附加一个斜杠 (\) 才能获得预期的输出。

      [s appendString:@"\"hi there == \"\n\r"];
      

      输出将是“你好 ==”

      【讨论】:

      • -​1 基本上只是在 4 年后重新发布现有答案的内容。顺便说一句,这里的意思是“前置”而不是“附加”。
      猜你喜欢
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 2012-03-12
      相关资源
      最近更新 更多