Line Separator in Java



Strings have no newlines. We can form them into two lines by concatenating a newline string. Use System lineSeparator to get a platform-dependent newline string.

The following is an example.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      String str = "one" + System.lineSeparator() + "two";
      System.out.println(str);
   }
}

Output

one
two

Let us see another example. On Linux based system, the program will work correctly.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      String str = System.lineSeparator();
      System.out.println((int) str.charAt(0));
   }
}

Output

10
Updated on: 2020-06-27T06:40:15+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started