字符串文字无需转义特殊字符?

发布于 2024-12-29 08:18:46 字数 145 浏览 0 评论 0 原文

Objective-c 中是否有不需要转义特殊字符的字符串文字形式?换句话说,我正在寻找与 Python 三重引号等效的内容。

我正在尝试将一些 HTML 放入 NSString 中,并且希望避免必须转义所有 HTML 属性中的引号。

Is there a string literal form in Objective-c that does not require escaping special characters? In other words, I'm looking for an equivalent to the Python triple quote.

I'm trying to put some HTML into an NSString, and would like to avoid having to escape the quotes from all the HTML attributes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

停滞 2025-01-05 08:18:48

对于现在阅读本文的任何人:

此功能自 Swift 4 以来已实现。在此处阅读有关它的更多信息:https://github.com/apple/swift-evolution/blob/master/proposals/0168-multi-line-string-literals.md

你可以这样做:

let author = "Im the author!"
let x = """
    <?xml version="1.0"?>
    <catalog>
        <book id="bk101" empty="">
            <author>\(author)</author>
            <title>XML Developer's Guide</title>
            <genre>Computer</genre>
            <price>44.95</price>
            <publish_date>2000-10-01</publish_date>
            <description>An in-depth look at creating applications with XML.</description>
        </book>
    </catalog>
"""

For anyone reading this now:

This feature has been implemented since Swift 4. Read more about it here: https://github.com/apple/swift-evolution/blob/master/proposals/0168-multi-line-string-literals.md

You can do:

let author = "Im the author!"
let x = """
    <?xml version="1.0"?>
    <catalog>
        <book id="bk101" empty="">
            <author>\(author)</author>
            <title>XML Developer's Guide</title>
            <genre>Computer</genre>
            <price>44.95</price>
            <publish_date>2000-10-01</publish_date>
            <description>An in-depth look at creating applications with XML.</description>
        </book>
    </catalog>
"""
故事与诗 2025-01-05 08:18:47

在 C++11 中你可以这样做。请参阅我对。 com/questions/1135841/c-multiline-string-literal">类似问题

为此,您需要 Objective-C++11。它应该可以在 gcc 中工作。

const char * html = R"HTML(
<HTML>
 <HEAD>
   <TITLE> [Python-Dev] Triple-quoted strings and indentation
   </TITLE>
 </HEAD>
 <BODY BGCOLOR="#ffffff">
  blah blah blah
 </BODY>
</HTML>
)HTML";

int
main()
{
}

g++ -std=c++0x -o raw_string raw_string.mm 至少可以编译。

In C++11 you can do this. See my answer to a similar question.

For this you require in your case Objective-C++11. It should work though in gcc.

const char * html = R"HTML(
<HTML>
 <HEAD>
   <TITLE> [Python-Dev] Triple-quoted strings and indentation
   </TITLE>
 </HEAD>
 <BODY BGCOLOR="#ffffff">
  blah blah blah
 </BODY>
</HTML>
)HTML";

int
main()
{
}

g++ -std=c++0x -o raw_string raw_string.mm at least compiles.

街道布景 2025-01-05 08:18:47

没有与三引号等效的东西;字符串文字必须始终对特殊字符使用转义符。

也许最好的办法是将 HTML 放入与源代码分开的文件中,然后使用 -[NSString initWithContentsOfFile:encoding:error:] (或相关的 initWithContentsOfURL:...)。

There's no equivalent to the triple-quote; string literals must always use escapes for special characters.

Perhaps the best thing to do would be to put your HTML into a file separate from your source, then create the string using -[NSString initWithContentsOfFile:encoding:error:] (or the related initWithContentsOfURL:...).

聊慰 2025-01-05 08:18:47

不,不幸的是没有......这里有一些关于 obj-c 中字符串文字的重要信息:
http://blog.ablepear.com/2010/07 /objective-c-tuesdays-string-literals.html

Nope, unfortunately not ... there's some great info on string literals in obj-c here:
http://blog.ablepear.com/2010/07/objective-c-tuesdays-string-literals.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文