Java String new line
In JavaScript, you can use the following special characters to add a new line in a string:
In Java, you can use the following special characters to add a new line in a string:
\n: This character is called a "line feed" and is used to move to the next line.
String str = "Hello, World!\nHow are you today?";
System.out.println(str);
// Output:
// Hello, World!
// How are you today?\r: This character is called a "carriage return" and is used to move the cursor to the beginning of the current line.
String str = "Hello, World!\rHow are you today?";
System.out.println(str);
// Output:
// How are you today?\r\n: This combination of characters is used to move to the next line and move the cursor to the beginning of the new line. This is the most commonly used new line character in Windows.
String str = "Hello, World!\r\nHow are you today?";
System.out.println(str);
// Output:
// Hello, World!
// How are you today?Note: In HTML, you can use the ```<br>``` element to add a new line. This applies to web/DOM rendering contexts, not standard Java console output.
String htmlSnippet = "Hello, World!<br>How are you today?";
// When rendered in a browser or HTML context, the string will display with a line break.