Home java How to add a line break to a String

How to add a line break to a String

Author

Date

Category

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

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

String s1 = "La-la-la-la-la-la" + "La-la-la- la-la ";

NeatBeans only offers string concatenation. And how do you write it down taking into account the line break.


Answer 1, authority 100%

String s1 = "La-la-la-la-la-la" + "\ n" + "La- La la la la la";

Answer 2, authority 50%

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

String s1 = "La-la-la-la-la-la" + System.lineSeparator () + "La -La la la la la";

Answer 3, authority 29%

String s1 = "La-la-la la-la-la \ nLa-la-la-la- la ";

(or the same, but with concatenation) will be the most common answer, and for many C-like languages.
Line feed may vary from platform to platform, but Java uses Unicode strings so works \ n .

However, in the case of formatted strings, to insert a platform-specific line separator, you need to use the “% n” character :

String s1 = String.format ("% s% n% s", "La-la-la- la-la "," La-la-la la-la-la ");

– (in this case, the second and third arguments of the format () method will be substituted for “% s” )


Answer 4, authority 14%

String song = "La-la-la \ n LaaaaaLaaaaa \ n";

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions