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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 27-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements