diff --git a/core-java/src/main/java/com/baeldung/string/AddingNewLineToString.java b/core-java/src/main/java/com/baeldung/string/AddingNewLineToString.java index e828af4cb7..0b3fd2ca92 100644 --- a/core-java/src/main/java/com/baeldung/string/AddingNewLineToString.java +++ b/core-java/src/main/java/com/baeldung/string/AddingNewLineToString.java @@ -7,49 +7,63 @@ public class AddingNewLineToString { String line2 = "Humpty Dumpty had a great fall."; String para = ""; + System.out.println("***New Line in a String in Java***"); //1. Using "\n" System.out.println("1. Using \\n"); - para = line1+"\n"+line2; + para = line1 + "\n" + line2; System.out.println(para); //2. Using "\r\n" System.out.println("2. Using \\r\\n"); - para = line1+"\r\n"+line2; + para = line1 + "\r\n" + line2; System.out.println(para); //3. Using "\r" System.out.println("3. Using \\r"); - para = line1+"\r"+line2; + para = line1 + "\r" + line2; System.out.println(para); //4. Using "\n\r" Note that this is not same as "\r\n" // Using "\n\r" is equivalent to adding two lines System.out.println("4. Using \\n\\r"); - para = line1+"\n\r"+line2; + para = line1 + "\n\r" + line2; System.out.println(para); //5. Using System.lineSeparator() System.out.println("5. Using System.lineSeparator()"); - para = line1+System.lineSeparator()+line2; + para = line1 + System.lineSeparator() + line2; System.out.println(para); //6. Using System.getProperty("line.separator") System.out.println("6. Using System.getProperty(\"line.separator\")"); - para = line1+System.getProperty("line.separator")+line2; + para = line1 + System.getProperty("line.separator") + line2; System.out.println(para); - //Line break for HTML using
- System.out.println("Line break for HTML using
"); - para = line1+"
"+line2; + System.out.println("***HTML to rendered in a browser***"); + //1. Line break for HTML using
+ System.out.println("1. Line break for HTML using
"); + para = line1 + "
" + line2; + System.out.println(para); - //Line break for HTML when string is in -
Humpty Dumpty sat on a wall.

Humpty Dumpty had a great fall
-
Humpty Dumpty sat on a wall.
Humpty Dumpty had a great fall
-
Humpty Dumpty sat on a wall.
Humpty Dumpty had a great fall
+ + +
+
Humpty Dumpty sat on a wall.
Humpty Dumpty had a great fall.
+
Humpty Dumpty sat on a wall.
Humpty Dumpty had a great fall.
+
Humpty Dumpty sat on a wall.
Humpty Dumpty had a great fall.
+
Humpty Dumpty sat on a wall.

Humpty Dumpty had a great fall.
+
Humpty Dumpty sat on a wall.
+Humpty Dumpty had a great fall
+

Humpty Dumpty sat on a wall.
Humpty Dumpty had a great fall

+

Humpty Dumpty sat on a wall. Humpty Dumpty had a great fall

+

Humpty Dumpty sat on a wall. Humpty Dumpty had a great fall

+

Humpty Dumpty sat on a wall. Humpty Dumpty had a great fall

+

Humpty Dumpty sat on a wall. +Humpty Dumpty had a great fall

- + \ No newline at end of file