在Java中我该怎么做这里的文档?像 BASH 或 PHP 一样,我们可以
我该如何使用Java。这里的文档像字符串吗?示例:
String java = << \EOF
#This file is written via Java
#You are watching JavaHereDoc
; comments ;
value=abc
etc etc
EOF;
System.out.println(java);显示与上面完全相同。我该怎么做?
How can i do with Java. Here doc like string? Exaple:
String java = << \EOF
#This file is written via Java
#You are watching JavaHereDoc
; comments ;
value=abc
etc etc
EOF;
System.out.println(java); shows exactly like above. How can i do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,Java(从 7 开始)不支持 HERE 文档(也称为多行字符串)。
如果您尝试完成模板化,有几个选项:
这些与 Perl 或 PHP 中的 HERE 文档并不完全相同,因为描述模板的字符串并不直接在您的代码中;它通常位于一个单独的文件中。
有一个提案提出Stephen Colebourne 以及 通过 Project Coin 提出的提案,但都没有进入 Java 7,这有点令人失望。 Groovy 和 Scala 等也在 JVM 上运行的语言确实支持多行字符串。
Java (as of 7) doesn't support HERE docs (also known as multiline strings) unfortunately.
If you're trying to accomplish templating, there are a few options:
These aren't exactly similar to HERE docs in Perl or PHP since the string that describes the template isn't directly in your code; it's usually in a separate file.
There was a proposal put forward by Stephen Colebourne as well as a proposal via Project Coin, neither of which made it into Java 7, which was a little disappointing. Languages like Groovy and Scala, which also run on the JVM do support multiline strings.
同时,如果您发现它很有用,这里有一个基本的 swing 应用程序,我用它来对嵌入到类中的 SQL 执行此操作。我发现自己一遍又一遍地使用它,为什么不分享它呢?
https://github.com/danielbchapman/Swing-String-Escaping-Utility
(一个小注释,它丢弃了代码,因为我在一周内将其组合在一起,我需要快速格式化查询,我可能会在某个时候清理它)
In the meantime if you find it useful here's a basic swing application I use to do this for SQL that's embedded into the classes. I find myself using it over and over again so why not share it?
https://github.com/danielbchapman/Swing-String-Escaping-Utility
(a small note, its throw away code since I put it together in a week where I needed to rapidly format queries, I might clean it up at some point)