Adding a Newline Character to a String in Java

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 Newline Characters in a String

Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”also called a Line FeedIn 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” , “\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 demonstrated three methods of adding a new line, but unfortunately, they’re platform dependent.

2.2. Using Platform Independent Line Separators

We can 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;

2.3. Using Platform Independent Newline Characters

Although line separators provide platform independence, they force us to concatenate our strings.

If we are using something like System.out.printf or String.format, then the platform independent newline character, %n, can be used directly within a string:

rhyme = "Humpty Dumpty sat on a wall.%nHumpty Dumpty had a great fall.";

This is the same as including System.lineSeparator() within our string, but we don’t need to divide the string into multiple parts.

3. Adding Newline Characters in an HTML Page

Suppose we are creating a string that 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.

Additionally, we can use “\n” in some HTML elements to break a line.

Overall, these are the three methods of breaking a line in HTML. We can decide which one to use 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. Newline 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

Finally, 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 + "
" + line2;
rhyme = line1 + "
" + line2;

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

rhyme = line1 + "
" + line2;
rhyme = line1 + "

" + line2;

4. The Difference Between \n and \r

\r and \n are characters denoted with ASCII values of 13 (CR) and 10 (LF), respectively. They both represent a break between two linesbut operating systems use them differently. 

On Windows, a sequence of two characters is used to start a new line, CR immediately followed by LF. Conversely, on Unix-like systems, only LF is used.

When writing Java applications, we must pay attention to the line break characters we use because the applications will behave differently depending on the operating system they will run on.

The safest and most cross-compatible option is to use System.lineSeparator(). This way, we won’t have to take the operating system into account.

5. Conclusion

In this article, we discussed how to add newline characters to a string in Java.

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

Finally, we wrapped up with how to add a new line in case we are generating an HTML page.

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