Logo

Programming-Idioms

  • Python
  • Java
String s = "This is a very long string which needs \n" +
           "to wrap across multiple lines because \n" +
           "otherwise my code is unreadable.";
import java.util.Formatter;
String s;
StringBuilder b = new StringBuilder();
Formatter f = new Formatter(b);
f.format("line 1%n");
f.format("line 2%n");
f.format("line 3%n");
f.format("line 4");
f.flush();
s = b.toString();
String s = """
This is a very long string which needs
to wrap across multiple lines because
otherwise my code is unreadable.
""";

Available since Java 15
import static java.lang.System.lineSeparator;
String s, n = lineSeparator();
s = "line 1" + n;
s = s + "line 2" + n;
s = s + "line 3" + n;
s = s + "line 4";
s = """Huey
Dewey
Louie"""
s = ('line 1\n'
     'line 2\n'
     'line 3\n'
     'line 4')

"... String literals that are part of a single expression and have only whitespace between them will be implicitly converted to a single string literal."
s : String := "Will this compile? " &
     "Oh yes it will";

I'm assuming this is what is meant by "consisting in several lines of text."

Use New_Line for line feeds.

New implementation...