Java多行字符串 [英] Java multiline string

查看:210
本文介绍了Java多行字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Perl,我肯定错过了here-document在源代码中创建多行字符串的方法:

Coming from Perl, I sure am missing the "here-document" means of creating a multi-line string in source code:

$string = <<"EOF"  # create a three-line string
text
text
text
EOF

在Java中,我必须在每一行都有繁琐的引号和加号,因为我从头开始连接多行字符串。

In Java, I have to have cumbersome quotes and plus signs on every line as I concatenate my multiline string from scratch.

有哪些更好的选择?在属性文件中定义我的字符串?

What are some better alternatives? Define my string in a properties file?

编辑:两个答案说StringBuilder.append()优于加号表示法。任何人都可以详细说明他们为什么这么认为?它看起来并不比我更好。我正在寻找一种方法,即多行字符串不是一流的语言构造,这意味着我绝对不希望用方法调用替换第一类语言构造(字符串连接加上)。

Edit: Two answers say StringBuilder.append() is preferable to the plus notation. Could anyone elaborate as to why they think so? It doesn't look more preferable to me at all. I'm looking for a way around the fact that multiline strings are not a first-class language construct, which means I definitely don't want to replace a first-class language construct (string concatenation with plus) with method calls.

编辑:为了进一步澄清我的问题,我根本不关心性能。我担心可维护性和设计问题。

Edit: To clarify my question further, I'm not concerned about performance at all. I'm concerned about maintainability and design issues.

推荐答案

Stephen Colebourne创建了一个提案,用于在Java 7中添加多行字符串。

Stephen Colebourne has created a proposal for adding multi-line strings in Java 7.

此外,Groovy已经支持多行字符串

Also, Groovy already has support for multi-line strings.

这篇关于Java多行字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆