Signup/Sign In
PUBLISHED ON: JULY 8, 2021

Adding a Newline Character to a String in Java

A string is an important data type in Java and we must know how to format strings to return multi-line outputs. Adding a new line to strings can be done in a lot of different ways. Newline characters are platform-dependent but Java also provides some platform-independent line separators that we can use in our Java code. Let's learn how to use them.

Adding a new line in java strings

Platform Dependent Newline Characters

\n or \r or a combination of both can be used to add a new line to a string. We simply need to add it where we want the output on a new line. For example, if we want to split "Java is awesome" into three different lines, the string would look like "Java\nis\nAwesome".

The \n is called the Line Feed(LF) and it is used to move the cursor to a new line. The \r is called the Carriage Return(CR) and is used to take the cursor to the beginning of the current line.

However, these newline characters are platform-dependent. Different operating systems define a new line in different ways.

  • For Windows, use \r\n(CRLF)

  • For Unix based OS(including modern Macs), use \n(LF)

  • For older Mac OS(before OS-X), use \r(CR)

Consider the following example that demonstrates the use of CRLF.

public static void main(String[] args) 
{  
    String str1 = "Java is awesome";
    System.out.println(str1);
    String str2 = "Java\r\nis\r\nawesome";
    System.out.print(str2);	       
}


Java is awesome
Java
is
awesome

Platform Independent Line Separators

The newline characters CR and LF can be used to add a new line but they are platform-dependent. Java provides us with a few methods that return the line separator according to the operating system.

This makes our code platform-independent.

We can use the System.lineSeparator() to add a new line in between strings.

public static void main(String args[])
{
 	String str1 = "Java is awesome";
 	System.out.println(str1);
 	String newline = System.lineSeparator();
 	String str2 = "Java" + newline + "is" + newline + "awesome";
 	System.out.print(str2);
}


Java is awesome
Java
is
awesome

Or we can use the getProperty() method with line.separator as a parameter.

public static void main(String args[])
{
    String str1 = "Java is awesome";
    System.out.println(str1);
    String newline = System.getProperty("line.separator");
    String str2 = "Java" + newline + "is" + newline + "awesome";
    System.out.print(str2);
}


Java is awesome
Java
is
awesome

Platform Independent Newline Characters

The %n is a platform-independent newline character and is a lot easier and simpler to use as compared to the other methods that we have discussed. Simply add %n whenever you want to move to a new line.

Make sure to use the printf() method to print the string to the console.

public static void main(String args[])
{
 	String str1 = "Java is awesome";
 	System.out.println(str1);
 	String str2 = "Java%nis%nawesome";
 	System.out.printf(str2);
}


Java is awesome
Java
is
awesome

Using System.out.println() Method

There is another way of adding strings to a new line when printing them. We can use the println() method to do this. It will first print the string to the console and then take the cursor to a new line. However, this is not a great way of formatting strings because our code can get very long if we have several strings.

For example, if we wish to print ten strings on ten different lines, then we will have to write System.out.println() ten different times.

public static void main(String args[])
{
    String str1 = "Java";
	String str2 = "is ";
 	String str3 = "awesome";
	System.out.println(str1);
 	System.out.println(str2);
 	System.out.print(str3);//Not using println() as we do not need a new line after this	 	
}


Java
is
awesome

Summary

We will be working with strings quite frequently and it is important to understand how to correctly format strings if we wish to return a multiline output. We can use platform-dependent newline characters like the Carriage Return and the Line Feed if we are sure that our code won't be used on some other operating system. Methods like the lineSeparator() and the getProperty() will return the appropriate line separator according to the OS.

%n is another newline character but it is platform-independent.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.