Javascript 什么是 \r",为什么要与 \n 一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10282314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:40:02  来源:igfitidea点击:

What is a \r" and why would one use it with a \n?

javascriptstring

提问by Danny Fox

What is the difference between this two strings?

这两个字符串有什么区别?

"first line \n second line", "first line \r\n second line"

"first line \n second line", "first line \r\n second line"

回答by adarshr

\r- Carriage Return
\n- Line Feed

\r- 回车 - 换
\n

\r\nis often used in preference to \nas it displays properly on both unix and Windows.

\r\n通常优先使用 ,\n因为它可以在 unix 和 Windows 上正确显示。

Carriage Return just returns the cursor to the beginning of the same line (without advancing to the next line) whereas a Line Feed feeds a new line.

回车只是将光标返回到同一行的开头(不前进到下一行),而换行则换行。

Note:(hover over the grey box below to find out)

注意:(将鼠标悬停在下面的灰色框上以查找)

If you ever get hold of a typewriter, try using it. It is such a magnificent piece of engineering which not many get to experience these days.

如果您曾经拥有打字机,请尝试使用它。这是一项如此宏伟的工程,如今很少有人能够体验到。

回答by Fabrizio Calderan

the difference is in the different EOLsystem (Unix like first, Win/DOS second)

区别在于不同的EOL系统(首先像 Unix,其次是 Win/DOS)

see http://en.wikipedia.org/wiki/Newline

http://en.wikipedia.org/wiki/Newline

回答by eabraham

Its the newline representation for different operating systems. In windows a new line is \r\n and Linux its \n.

它是不同操作系统的换行符。在 Windows 中,新行是 \r\n,而 Linux 是 \n。