Skip to content

Commit 6dd09ae

Browse files
committed
New line in Java for HTML and Changing HTML file
1 parent b9b4cec commit 6dd09ae

2 files changed

Lines changed: 55 additions & 22 deletions

File tree

‎core-java/src/main/java/com/baeldung/string/AddingNewLineToString.java‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,63 @@ public static void main(String[] args) {
77
String line2 = "Humpty Dumpty had a great fall.";
88
String para = "";
99

10+
System.out.println("***New Line in a String in Java***");
1011
//1. Using "\n"
1112
System.out.println("1. Using \\n");
12-
para = line1+"\n"+line2;
13+
para = line1 + "\n" + line2;
1314
System.out.println(para);
1415

1516
//2. Using "\r\n"
1617
System.out.println("2. Using \\r\\n");
17-
para = line1+"\r\n"+line2;
18+
para = line1 + "\r\n" + line2;
1819
System.out.println(para);
1920

2021
//3. Using "\r"
2122
System.out.println("3. Using \\r");
22-
para = line1+"\r"+line2;
23+
para = line1 + "\r" + line2;
2324
System.out.println(para);
2425

2526
//4. Using "\n\r" Note that this is not same as "\r\n"
2627
// Using "\n\r" is equivalent to adding two lines
2728
System.out.println("4. Using \\n\\r");
28-
para = line1+"\n\r"+line2;
29+
para = line1 + "\n\r" + line2;
2930
System.out.println(para);
3031

3132
//5. Using System.lineSeparator()
3233
System.out.println("5. Using System.lineSeparator()");
33-
para = line1+System.lineSeparator()+line2;
34+
para = line1 + System.lineSeparator() + line2;
3435
System.out.println(para);
3536

3637
//6. Using System.getProperty("line.separator")
3738
System.out.println("6. Using System.getProperty(\"line.separator\")");
38-
para = line1+System.getProperty("line.separator")+line2;
39+
para = line1 + System.getProperty("line.separator") + line2;
3940
System.out.println(para);
4041

41-
//Line break for HTML using <br>
42-
System.out.println("Line break for HTML using <br>");
43-
para = line1+"<br>"+line2;
42+
System.out.println("***HTML to rendered in a browser***");
43+
//1. Line break for HTML using <br>
44+
System.out.println("1. Line break for HTML using <br>");
45+
para = line1 + "<br>" + line2;
46+
System.out.println(para);
47+
48+
//2. Line break for HTML using “&#10;”
49+
System.out.println("2. Line break for HTML using &#10;");
50+
para = line1 + "&#10;" + line2;
51+
System.out.println(para);
4452

45-
//Line break for HTML when string is in <textarea> tag
46-
para = line1+"&#10;"+line2;
47-
para = line1+"&#13;"+line2;
53+
//3. Line break for HTML using “&#13;”
54+
System.out.println("3. Line break for HTML using &#13;");
55+
para = line1 + "&#13;" + line2;
56+
System.out.println(para);
4857

49-
//Line break for HTML when string is in <pre> tag
50-
para = line1+"&#10;"+line2;
51-
para = line1+"&#10;&#13;"+line2;
52-
para = line1+"<br>"+line2;
58+
//4. Line break for HTML using “&#10&#13;;”
59+
System.out.println("4. Line break for HTML using &#10;&#13;");
60+
para = line1 + "&#10;&#13;" + line2;
61+
System.out.println(para);
62+
63+
//5. Line break for HTML using \n”
64+
System.out.println("5. Line break for HTML using \\n");
65+
para = line1 + "\n" + line2;
66+
System.out.println(para);
5367
}
5468

55-
}
69+
}
Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
<html>
22
<body>
3-
Humpty Dumpty sat on a wall.<br>Humpty Dumpty had a great fall.
3+
Humpty Dumpty sat on a wall.<br>Humpty Dumpty had a great fall.<br>
4+
Humpty Dumpty sat on a wall.&#10;Humpty Dumpty had a great fall.<br>
5+
Humpty Dumpty sat on a wall.&#13;Humpty Dumpty had a great fall.<br>
6+
Humpty Dumpty sat on a wall.&#10;&#13;Humpty Dumpty had a great fall.<br>
7+
Humpty Dumpty sat on a wall.
8+
Humpty Dumpty had a great fall.
49
<br>
10+
<textarea>Hello<br>Baeldung</textarea>
511
<textarea>Hello&#10;Baeldung</textarea>
612
<textarea>Hello&#13;Baeldung</textarea>
7-
<pre>Humpty Dumpty sat on a wall.&#10;&#13;Humpty Dumpty had a great fall</pre>
8-
<pre>Humpty Dumpty sat on a wall.&#13;Humpty Dumpty had a great fall</pre>
9-
<pre>Humpty Dumpty sat on a wall.&#10;Humpty Dumpty had a great fall</pre>
13+
<textarea>Hello&#10;&#13;Baeldung</textarea>
14+
<textarea>Hello.
15+
Baeldung</textarea>
16+
<br>
17+
<pre>Humpty Dumpty sat on a wall.<br>Humpty Dumpty had a great fall.</pre>
18+
<pre>Humpty Dumpty sat on a wall.&#10;Humpty Dumpty had a great fall.</pre>
19+
<pre>Humpty Dumpty sat on a wall.&#13;Humpty Dumpty had a great fall.</pre>
20+
<pre>Humpty Dumpty sat on a wall.&#10;&#13;Humpty Dumpty had a great fall.</pre>
21+
<pre>Humpty Dumpty sat on a wall.
22+
Humpty Dumpty had a great fall</pre>
23+
<br>
1024
<p>Humpty Dumpty sat on a wall.<br>Humpty Dumpty had a great fall</p>
25+
<p>Humpty Dumpty sat on a wall.&#10;Humpty Dumpty had a great fall</p>
26+
<p>Humpty Dumpty sat on a wall.&#13;Humpty Dumpty had a great fall</p>
27+
<p>Humpty Dumpty sat on a wall.&#10;&#13;Humpty Dumpty had a great fall</p>
28+
<p>Humpty Dumpty sat on a wall.
29+
Humpty Dumpty had a great fall</p>
1130
</body>
12-
</html>
31+
</html>

0 commit comments

Comments
 (0)