Home
Java
Newline Examples: System.lineSeparator
This page was last reviewed on Dec 6, 2021.
Dot Net Perls
Newlines. Two strings have no newlines. We can compose them into two lines by concatenating a newline string. In Java we can add newlines in many ways.
With System lineSeparator, we get a platform-dependent newline string. With Character.SEPARATOR we access a line break value. Often using "\n" directly is a good choice.
System.lineSeparator. This program uses System.lineSeparator. On a Windows computer this is equal to \r\n. On a Mac (or Linux) system it equals just "\n."
Detail We combine the two strings "cat" and "dog" with a lineSeparator() call in between. This composes the strings into two lines.
public class Program { public static void main(String[] args) { // ... Use System.lineSeparator. String result = "cat" + System.lineSeparator() + "dog"; System.out.println(result); } }
cat dog
LineSeparator, Windows, UNIX. Let us examine the values returned by lineSeparator. This program, when run on a Windows computer, should display both chars in the lineSeparator string.
And On a UNIX-based system like Mac or Linux, the program should instead display "One char."
public class Program { public static void main(String[] args) { String newline = System.lineSeparator(); // Figure out the contents of this string. if (newline.length() == 2) { System.out.println("Two chars"); System.out.println((int) newline.charAt(0)); System.out.println((int) newline.charAt(1)); } else if (newline.length() == 1) { System.out.println("One char"); System.out.println((int) newline.charAt(0)); } } }
Two chars 13 10
Character. This example uses the Character.LINE_SEPARATOR as a line break. This inserts the character "\r" in between the two strings.
Warning It is probably better and more standard to use the constant "\n" for a line break.
Detail We must cast the LINE_SEPARATOR value to add it to a string. Otherwise it is displayed as an integer.
public class Program { public static void main(String[] args) { // ... Use Character value. // The cast is important. String result = "one" + (char) Character.LINE_SEPARATOR + "two"; System.out.println(result); } }
one two
Character, LINE_SEPARATOR value. Here I show that the LINE_SEPARATOR constant is equal to the value 13, which is the value "\r." This is not a "\n" value.
public class Program { public static void main(String[] args) { // The LINE_SEPARATOR equals the \r char. System.out.println((int) Character.LINE_SEPARATOR); System.out.println((int) '\r'); } }
13 13
Newline literal. This program directly uses the "\n" character to create a line break. This is a clear approach to the problem. It can also be optimized into a single literal by a compiler.
public class Program { public static void main(String[] args) { // ... Use concat with newline string. String result = "bird" + "\n" + "fish"; System.out.println(result); } }
bird fish
BufferedWriter, newLine. When writing a text file with BufferedWriter, we can use the newline method. This inserts the line-break into the output text.
BufferedWriter
A summary. We added newlines to strings with concatenation. We use System.lineSeparator, a Character constant, and the newline literal.
Character
For the lineSeparator method, we get a two-char string on Windows systems. This is longer than a "\n" character. For the shortest and simplest line breaks, use a "\n" char.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 6, 2021 (edit link).
Home
Changes
© 2007-2024 Sam Allen.