Friday, January 31, 2025
Google search engine
HomeLanguagesJavaSystem.lineSeparator() method in Java With Examples

System.lineSeparator() method in Java With Examples

The lineSeparator() is a built-in method in Java which returns the system-dependent line separator string. It always returns the same value ā€“ the initial value of the system property line.separator.

Syntax:

public static String lineSeparator()

Parameters: This method does not take any parameters.

Return Values: In any UNIX systems, it will return ā€œ\nā€ or a positive integer; and on Windows systems it returns ā€œ\r\nā€ or a positive integer.

Exception : It throws NullPointerException , if the string is null

Below programs illustrate the System.lineSeparator() method:

Program 1: To illustrate the working of static String lineSeparator() method.




// Java program to demonstrate working
// of static String lineSeparator() method
import java.io.IOException;
import java.lang.*;
import java.nio.channels.Channel;
public class LineSeparatorExample {
Ā Ā 
Ā Ā Ā Ā public static void main(String[] args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā String s = System.lineSeparator();
Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā for (char c : s.toCharArray()) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println((int)c);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}


Output:

10

Note: Here it returns 10. So here 10 is the line separator.

Program 2: To illustrate the working of static String lineSeparator() method for an integral value.




// Java program to demonstrate working
// of static String lineSeparator() method
import java.io.IOException;
import java.lang.*;
import java.nio.channels.Channel;
class SystemDemo {
Ā Ā Ā Ā public static void main(String args[])
Ā Ā Ā Ā Ā Ā Ā Ā throws NullPointerException,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā IOException
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Integer x = 636;
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(System.lineSeparator());
Ā Ā Ā Ā }
}


Output:

\r\n

Note: Here it returns ā€œ\r\nā€ since its a Microsoft Windows systems.

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#lineSeparator()

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?