Java 是否支持多行字符串? [英] Does Java have support for multiline strings?

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

问题描述

来自 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
EOF

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?

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 是否支持多行字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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