Java Top

Finally announcing my next course. The intro price of the upcoming “Learn Spring” course will permanently increase by $50 on Friday:

>> HAVE A LOOK

1. Overview

String formatting and generating text output often comes up during programming. In many cases, there is a need to add a new line to a string to format the output.

Let’s discuss how to use newline characters.

2. Adding New Line in a String

Operating systems have special characters to denote the start of a new line. For example, in Linux, a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.

Adding a new line in Java is as simple as including “\n” or “\r” or “\r\n” at the end of our string.

2.1. Using CRLF Line-Breaks

For this example, we want to create a paragraph using two lines of text. Specifically, we want line2 to appear in a new line after line1.

For a Unix/Linux/New Mac-based OS we can use “\n”:

String line1 = "Humpty Dumpty sat on a wall.";
String line2 = "Humpty Dumpty had a great fall.";
String rhyme = line1 + "\n" + line2;

If we are on a Windows based OS, we can use “\r\n”:

rhyme = line1 + "\r\n" + line2;

For an Old Mac based OS, we can use “\r”:

rhyme = line1 + "\r" + line2;

We’ve shown three methods of adding a new line, but it’s too bad that it would be platform-dependent.

2.2. Using Platform-Independent Line Breaks

We can also use system-defined constants when we want our code to be platform independent.

For example, using System.lineSeparator() for giving a line separator:

rhyme = line1 + System.lineSeparator() + line2;

Or we could also use System.getProperty(“line.separator”):

rhyme = line1 + System.getProperty("line.separator") + line2;

3. Adding New Line in an HTML

Suppose we are creating a string which is part of an HTML page. In that case, we can add an HTML break tag <br>.

We can also use Unicode characters “& #13;” (Carriage Return) and “& #10;” (Line Feed). Although these characters work, they don’t work exactly like we might expect them to across all platforms. Instead, it is better to use <br> for line breaks.

We can also use “\n” in some HTML elements to break a line.

Overall there are three methods of breaking a line in HTML. We can decide to use one of them, depending on the HTML tag we are using.

3.1. HTML Break Tag

We can use HTML break tag <br> to break a line:

rhyme = line1 + "<br>" + line2;

The <br> tag for breaking a line, works in almost all HTML elements like <body>, <p>, <pre> etc. However, note that it does not work in the <textarea> tag.

3.2. New Line Character

We can use ‘\n’ to break a line, if text is enclosed in <pre> or <textarea> tag:

rhyme = line1 + "\n" + line2;

3.3. Unicode Characters

We can use Unicode characters “& #13;” (Carriage Return) and “& #10;” (Line Feed), to break a line. For example, in the <textarea> tag, we can use either of these:

rhyme = line1 + "&#10;" + line2;
rhyme = line1 + "&#13;" + line2;

For the <pre> tag, both of the lines below will work:

rhyme = line1 + "&#10;" + line2;
rhyme = line1 + "&#10;&#13;" + line2;

4. Conclusion

In this article, we discussed how to add new line character in a string in Java.

We also saw how to write platform-independent code for new line using System.lineSeparator() and System.getProperty(“line.separator”).

And finally, we wrapped up with how to add a newline in case we are generating HTML page.

The full implementation of this tutorial can be found over on GitHub.

Java bottom

Finally announcing my next course. The intro price of the upcoming “Learn Spring” course will permanently increase by $50 on Friday:

>> HAVE A LOOK