Java System.lineSeparator() – Examples

In this tutorial, we will learn about the Java System.lineSeparator() function, and learn how to use this function to get the line separator as String, with the help of examples.

lineSeparator()

System.lineSeparator() returns the system-dependent line separator as String.

Syntax

The syntax of lineSeparator() function is

</>
Copy
lineSeparator()

Returns

The function returns String.

Example 1 – lineSeparator()

In this example, we will get the line separator using System.lineSeparator() and use this line separator to join two string values: "a" and "b".

Java Program

</>
Copy
public class Example {
	public static void main(String args[]) {
		String lineSeparator = System.lineSeparator();
		System.out.println("a" + lineSeparator + "b"); 
	} 
}

Output

a
b

Example 2 – lineSeparator() – Characters in lineSeparator

In this example, we will print the characters in the String returned by System.lineSeparator() method.

Java Program

</>
Copy
public class Example {
	public static void main(String args[]) {
		String lineSeparator = System.lineSeparator();
		lineSeparator.chars().forEach(ch -> {
			System.out.println((int)ch);
		});
	} 
}

Output

13
10

13 is the return feed and 10 is the new line character.

Conclusion

In this Java Tutorial, we have learnt the syntax of Java System.lineSeparator() function, and also learnt how to use this function with the help of examples.