java multiline_Java 7 - Multiline strings

问题

I've read that multiline string literals were proposed to be added in Java 7.

Although I can't find any documentation saying definitely that they have been. I'd like to know if they are, because this is something I'd consider switching versions for.

回答1:

Multiline string literals are not going to be added to JDK 7. You can check Project Coin's homepage for a list of language changes.

However, you can use Scala, which does support multiline string literals using triple quotes:

var s = """Hello

World"""

回答2:

Multiline strings were not added into Java (even as of Java 8, the newest current version), and probably will never be added to Java. However, you can add multiple strings together like so:

String greeting = "Hello " +

"world! " +

"This is a multiline string.";

Or, if you want the multiline line breaks to actually start a new line, insert "\n" to the end of each line.

回答3:

Following Java's coding conventions Strings should be concatenated like:

String str = "Long text line "

+ "more long text.";

Make sure the + operator always begins the next line for readability.

See: Code Conventions for the Java Programming Language: 4. Indentation

回答4:

Multiline strings are supported in Java since JDK 13. They are called text blocks:

String html = """

Hello, world

""";

Note, this is a preview feature. But I hope it will become a permanent feature in one of the next releases (JDK 14-15).

来源:https://stackoverflow.com/questions/4581338/java-7-multiline-strings

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值