2

How can I print to console system sign for line separator like \n or \r; I know that System.getProperty("line.separator") returns the line separator and it breaks the line but I'd like to show what sign it is. Like \n or \r.

I want console result to be:

line separator is: \n

I have this code:

System.out.println("Line separator: " + System.getProperty("line.separator"));

It Shows

Line separator:

and then breaks the line. I would like a function to show me a sign (\n or \r).

  • Mark this to delete – Przemysław Głębocki Aug 12 '19 at 15:44
  • May I ask for reason you wish to delete this question? If it is fact that is was marked as duplicate then there is no need to be worried about it. Duplicate system was introduced as "road signs" for people who are facing same problem but can't find our canonical question and answer about it because they used different problem description (like here "show line separator" instead of more precise "escape line separator"). Because such *similar* questions exist they can find them instead and follow duplicate link. – Pshemo Aug 12 '19 at 16:36
  • Yeah I made that comment and then tried to delete and came upon the same explanation. Thx for clarifying ;) – Przemysław Głębocki Aug 12 '19 at 20:12

1 Answers1

3

Simply replace returned line separators (\n or \r) with strings representing \ and n or r. To create \ character you need to escape it with another \ (since \ is special in String literals - it can be used for instance to create some special characters like tab \t, or characters using their hexadecimal form \uFFFF, or simply to escape itself).

System.out.println("Line separator: " + System.getProperty("line.separator")
                .replace("\r", "\\r")//replace `\r` literal with `\` and `r`
                .replace("\n", "\\n"));//same about `\n`

which in my case prints

Line separator: \r\n
Pshemo
  • 122,468
  • 25
  • 185
  • 269