简体   繁体   English

Java 等价于 C# 的带有 @ 的逐字字符串

[英]Java equivalent of C#'s verbatim strings with @

Quick question.快速提问。 Is there an equivalent of @ as applied to strings in Java:是否有适用于 Java 字符串的 @ 等价物:

For example I can do @"c:\\afolder\\afile" in C# and have it ignore the escape characters when processing instead of having to do "c:\\\\afolder\\\\aFile" .例如,我可以在 C# 中执行@"c:\\afolder\\afile"并让它在处理时忽略转义字符,而不必执行"c:\\\\afolder\\\\aFile" Is there a Java equivalent?有Java等价物吗?

hmmm: stackoverflow is escaping on me .. lol.嗯:stackoverflow 正在逃避我.. 大声笑。 The second example should read:第二个例子应该是:

c:(double-backslash)afolder(double-backslash)aFile c:(double-backslash)afolder(double-backslash)aFile

No .没有 Escaping / externalizing the string is your only choice.转义/外部化字符串是您唯一的选择。

No, Java doesn't have verbatim string literals .不,Java 没有逐字字符串文字

If you want a Java-like (and Java-VM-based) language that does , however, you might want to look at Groovy which has various forms of string literal .如果你想,一个Java类(和基于Java VM)语言,然而,你可能想看看在Groovy中拥有各种形式的字符串文字的

As Kent and Jon have said, no there isn't.正如肯特和乔恩所说,不,没有。

I'm answering just to point out that even if there were, for your particular case, it would be a bad idea in the general case, assuming a more than one-off program.我的回答只是为了指出,即使有,对于您的特定情况,在一般情况下,假设一个不止一次的计划,这也是一个坏主意。

Java programs run on more platforms than just Windows, and other platforms have different file delimiters. Java 程序运行在更多平台上,而不仅仅是 Windows,其他平台有不同的文件分隔符。 So instead of dealing with escaped backslashes, the correct way to handle your particular example is by getting the file separator property:因此,处理您的特定示例的正确方法是获取文件分隔符属性,而不是处理转义的反斜杠:


    String sep = System.getProperty("file.separator");
    String filename = ROOTDIR + sep + "folder" + sep + "afile";

Where you'd have separately created ROOTDIR based on some policy - not only the platform, but whether you want your "afile" to be relative to the actual file system root, or relative to the user's home directory.您将根据某些策略单独创建 ROOTDIR - 不仅是平台,还包括您希望“afile”相对于实际文件系统根目录,还是相对于用户的主目录。

But definitely, using the file separator property makes your programs more widely usable.但可以肯定的是,使用文件分隔符属性会使您的程序更广泛地使用。 Is it more work?这是更多的工作吗? Yes.是的。 As Wanda Sykes says, "But it's worth it".正如万达赛克斯所说,“但这是值得的”。

Currently it is not supported in Java but could be available in future releases.目前它在 Java 中不受支持,但可能在未来版本中可用。 There was created JEP 326: Raw String Literals at 2018/01/23在 2018/01/23 创建了JEP 326: Raw String Literals

See the progress at https://bugs.openjdk.java.net/browse/JDK-8196004https://bugs.openjdk.java.net/browse/JDK-8196004查看进度

Probably some day you will be able to do it with:也许有一天你将能够做到:

`c:\afolder\afile`

UPDATE: JEP proposed to drop from JDK 12:326: Raw String Literals (Preview) You can read the rationale here: http://mail.openjdk.java.net/pipermail/jdk-dev/2018-December/002402.html更新: JEP 建议从 JDK 12:326:Raw String Literals(预览)中删除您可以在这里阅读基本原理: http : //mail.openjdk.java.net/pipermail/jdk-dev/2018-December/002402.html

And more details here https://bugs.openjdk.java.net/browse/JDK-8215682以及更多详细信息https://bugs.openjdk.java.net/browse/JDK-8215682

The bottom line : There will not be verbatim strings in Java in near future.底线:在不久的将来,Java 中不会有逐字字符串。 And even if it will appear it rather will not be ``.即使它会出现,它也不会是``.

Java has it since September 2019, but it used different sintax. Java 自 2019 年 9 月以来就拥有它,但它使用了不同的语法。 “”” (three double-quote marks). “””(三个双引号)。

*In more recent java versions (+13 in preview, +15 as production ready), mostly equivalent can be achieved with java text blocks. *在更新的 Java 版本中(预览版 +13,生产就绪版 +15),使用 Java 文本块可以实现大部分等效。

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

Documentation on https://docs.oracle.com/en/java/javase/13/text_blocks/index.html https://docs.oracle.com/en/java/javase/13/text_blocks/index.html 上的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM