来自 Perl,我肯定缺少在源代码中创建多行字符串的here-document"方法:
$string = <<"EOF" # 创建一个三行字符串文本文本文本EOF在 Java 中,当我从头开始连接多行字符串时,我必须在每一行都有繁琐的引号和加号.
有哪些更好的选择?在属性文件中定义我的字符串?
编辑:两个答案说 StringBuilder.append() 比加号更可取.谁能详细说明他们为什么这么认为?它看起来一点也不对我更可取.我正在寻找一种方法来解决多行字符串不是一流的语言构造这一事实,这意味着我绝对不想用方法调用替换一流的语言构造(带加号的字符串连接).
编辑:为了进一步澄清我的问题,我根本不关心性能.我担心可维护性和设计问题.
解决方案Stephen Colebourne 创建了一个 建议 在 Java 7 中添加多行字符串.
此外,Groovy 已经支持多行字符串.
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 EOFIn 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?
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 has created a proposal for adding multi-line strings in Java 7.
Also, Groovy already has support for multi-line strings.
更多推荐
Java 是否支持多行字符串?
发布评论