2

This may be a simple question, but I was unable to find anything resembling the topic:

I have a program that needs to be run in Linux, and Windows. As you know, Windows new line character is \r\n, and in Linux/Unix it is \n (Mac is \r).

When I need to print anything to a file or to the command line, I can use println() function to create a new line. This works fine, but I need to return many new line characters as Strings so that I can for example use them more than once, modify them, etc. (I can also use find-replace to replace all my \r\n at the same time.)

The windows character \r\n I am using currently prints ^M in Linux.

Is there a character, or method I can use to return new lines in Strings?

gagarwa
  • 1,426
  • 1
  • 15
  • 28

2 Answers2

6

use System.lineSeparator() it works in all platforms.

Jishnu Prathap
  • 1,955
  • 2
  • 21
  • 37
4

use System.getProperty("line.separator") that returns system specific line separator.

Parth Soni
  • 11,158
  • 4
  • 32
  • 54