How to add a line break to a String


Please tell me how I can write such a string in String

La-la-la la-la-la
La-la-la la-la-la

String s1="Ля-ля-ля ля-ля-ля"+"Ля-ля-ля ля-ля-ля";

NeatBeans only offers string concatenation. And how to still write with a line break in mind.

Author: Nick Volynkin, 2012-10-02

4 answers

String s1 = "Ля-ля-ля ля-ля-ля"+ "\n" + "Ля-ля-ля ля-ля-ля";
 14
Author: skegg, 2012-10-02 15:46:08

Use System.lineSeparator(). This will do a line feed on both Windows and Linux.

String s1="Ля-ля-ля ля-ля-ля"+System.lineSeparator()+"Ля-ля-ля ля-ля-ля";
 7
Author: Александр Колобов, 2019-12-23 13:02:48
String s1 = "Ля-ля-ля ля-ля-ля\nЛя-ля-ля ля-ля-ля";

(or the same, but with concatenation) will be the most frequent answer, and for many C-like languages. The line feed may be different on different platforms and their versions, but Java uses Unicode strings, so works \n.

However, in the case of formatted strings, to insert a platform-dependent string separator, must use the character "%n":

String s1 = String.format("%s%n%s", "Ля-ля-ля ля-ля-ля", "Ля-ля-ля ля-ля-ля");

-- (in this case, in place of "%s " will be substituted the second and third arguments of the format () )

 4
Author: Nimtar, 2019-01-22 20:00:04
String song = "La-la-la \n LaaaaaLaaaaa \n";
 2
Author: EvGen, 2016-02-11 14:22:01