What is the actual cost of "line.separator"?

I always think that line.separatorperforms the same actions as \n, and yes, that . but I noticed that both values ​​actually don't match (maybe they don’t even close?) when I check this code:

String str1 = System.getProperty("line.separator");
String str2 = "\n";

System.out.println(str1.equals(str2)); // output:false (as expected)

Then I checked the length of both:

System.out.println(str1.length());     //output: 2
System.out.println(str2.length());     //output: 1

I wonder why str1.length()there is 2, I tried this:

System.out.println("**"+str1.charAt(0)+"**");

exit:

**

System.out.println("##"+str1.charAt(1)+"##");

exit:

##
##

So, I noticed that the actual newline character in line.separatoris the second character. Then what is the value in the first index, since when it should print ****(at least), it prints instead **?

+4
source share
4 answers

Depends on your OS.

line.separator \r\n

System.out.println(str1.length()); //output: 2, , Windows ( : 1)

, :

System.out.println("**"+str1.charAt(0)+"**");

:

**

\r - ** **, **. , **

:

System.out.println("##"+str1.charAt(1)+"##");

:

##
##

, str1.charAt(1) \n

+5

/, Linux \n, Windows \r\n

Windows: '\r\n'
Mac (OS 9-): '\r'
Mac (OS 10+): '\n'
Unix/Linux: '\n'
+4

Windows "\ r\n", * NIX, "\n", pre-OSX Mac "\ r". System, .

+3

- LF -\n - 0x0a - 10 ()

- CR -\r - 0x0D - 13 ()

, , , : ?

. . Mac '\ r , Unix Linux '\n . , "\ r\n". , , .

0

Source: https://habr.com/ru/post/1525896/


All Articles