0
String s = System.lineSeparator();
System.out.println(s);

I try to obtain text from variable s, but why is there no text in variable s ?

1
  • 6
    Oddly enough, System.lineSeparator() returns a line separator. Surprised me too. Commented Feb 13, 2017 at 11:24

3 Answers 3

1

Try following code on your system

for(byte b : System.lineSeparator().getBytes()){
    System.out.println(b);
}

It will print either

10

OR

13
10

Here I print the ascii code for whatever I got from System.lineSeparator().
ascii code for \n is 10 and for \r is 13.

It is also given in documentation of System.lineSeparator()

On UNIX systems, it returns "\n"; on Microsoft Windows systems it returns "\r\n".

So the point is you didn't see any output because if you try to print \r or \n because \r represents line feed and \n represents next line. And you cannot see them on console. But they will have their effects in strings.

Sign up to request clarification or add additional context in comments.

1 Comment

Whether have a way to print \r\n or \n ?
0

The System.lineSeparator(); returns a string that the system uses to generally separate lines in, say for example, an input from the Standard Input.

This is generally a new line character and so when you print it in your program, you will not "See" it as is.

Try using this:

String s = System.lineSeparator();
System.out.println("~~" + s + "~~");

This will help you distinguish the output. You should see something like this:

~~
~~

This output would indicate the the new line character is separating the ~~ characters in your print statement.


Hope this helps!

Comments

0

System.lineSeperator() will mostly return "\r\n", so when you sysout it actualy prints a new line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.