Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

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.

Further reading:

Checking for Empty or Blank Strings in Java

Check out some simple ways in Java to test if a string is blank or empty.

Check If a String Contains a Substring

Explore various ways to search for a substring in a String with performance benchmarks

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 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 New Lines

But, while 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 new line 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 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 + "
" + 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 lines, but operating systems use them differently. 

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

When writing Java applications, we must take care of the line break characters we use given the fact the application will behave differently depending on the operating system they'll run on.

The safest and 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 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

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!