Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 901
Mike Avatar asked Jan 03 '11 00:01

Mike


3 Answers

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"""
like image 146
João Silva Avatar answered Sep 24 '22 01:09

João Silva


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.

like image 35
hyper-neutrino Avatar answered Sep 23 '22 01:09

hyper-neutrino


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

String html = """
          <html>
              <body>
                  <p>Hello, world</p>
              </body>
          </html>
          """;

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

like image 28
ZhekaKozlov Avatar answered Sep 26 '22 01:09

ZhekaKozlov