Line Separator in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 17-Dec-2018 10:26:10
Advertisements