Java 有“@”吗?转义字符串引号的字符?

发布于 2024-08-16 22:13:48 字数 119 浏览 8 评论 0原文

我的字符串中有双引号,在 C# 中我会这样做:

string blah = @"this is my ""text";

在 Java 中我将如何做到这一点?

My string has double quotes in it, in C# I would do:

string blah = @"this is my ""text";

how would I do that in Java?

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

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

发布评论

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

评论(3

妄断弥空 2024-08-23 22:13:48

不可以。Java 中没有这样的功能。
来自 Sun docs

遇到转义序列时在 print 语句中,编译器会相应地解释它。例如,如果您想在引号内放置引号,则必须在内部引号上使用转义序列 \"。要打印

She said "Hello!" to me.

您将编写的句子

System.out.println("She said \"Hello!\" to me.");

No. Such a feature is not available in Java.
From the Sun docs:

When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. For example, if you want to put quotes within quotes you must use the escape sequence, \", on the interior quotes. To print the sentence

She said "Hello!" to me.

you would write

System.out.println("She said \"Hello!\" to me.");
孤檠 2024-08-23 22:13:48

目前还没有类似的东西,但这是一个“修复理解”的增强请求。

如果添加,此功能将允许这样的字符串文字:

String qry = @"
   SELECT
     a.name, b.number
   FROM
     User a, Data b
   WHERE
     a.name = "James"
      AND
     a.id = b.id
";

一些评论拒绝这一点,主要是因为它是一种语法糖,但显然对它的需求很高,所以有一天我们可能会看到它。

There is nothing like it currently, but it is a "Fix understood" request for enhancement.

If added, this feature would allow such string literals:

String qry = @"
   SELECT
     a.name, b.number
   FROM
     User a, Data b
   WHERE
     a.name = "James"
      AND
     a.id = b.id
";

Some of the comments reject this principally on the fact that it's a syntactic sugar, but obviously there's high demand for it, so it's possible that we'll see this someday.

熟人话多 2024-08-23 22:13:48

是*(自 2019 年 9 月起),但它使用不同的语法。 “””(三个双引号)。

*在最近的java版本中(预览版+13,生产就绪+15),大多数等效可以通过java文本块实现。

String html = """         
            <xml>
                <ody>
                    <pan>example xml </pan>
                </ody>
            </xml>""";

https://docs.oracle.com/en/java/ javase/13/text_blocks/index.html

Yes* (since September 2019) but it used different sintax. “”” (three double-quote marks).

*In more recent java versions (+13 in preview, +15 as production ready), mostly equivalent can be achieved with the java text blocks.

String html = """         
            <xml>
                <ody>
                    <pan>example xml </pan>
                </ody>
            </xml>""";

https://docs.oracle.com/en/java/javase/13/text_blocks/index.html

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