\n 和 \r\n 有什么区别?

发布于 2024-09-25 13:20:48 字数 38 浏览 8 评论 0原文

它们都意味着“新线”,但是什么时候使用其中一个而不是另一个呢?

They both mean "new line" but when is one used over the other?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

感性不性感 2024-10-02 13:20:48

\r\nWindows 样式

\nPOSIX 样式

\r 是旧的OS X 之前的Mac 风格,现代Mac 使用POSIX 风格。


\r 是回车符,\n 是换行符。在没有显示器的旧计算机上,您需要使用纸张和打印机才能将程序的结果提供给用户。为了使打印文件中的新行从最左边的列开始,文件需要用于换行\n\r 对于 get 回车到最左边的位置。这是从过去的计算机中遗留下来的,现在通常只能在 Windows 上看到。

\r\n is a Windows Style

\n is a POSIX Style

\r is a old pre-OS X Macs Style, Modern Mac's using POSIX Style.


\r is a carriage return and \n is a line feed. On old computers without monitors, you needed to use paper and a printer to get a program's result to the user. In order for new lines in your printed file to start at the leftmost column, files needed both a \n for line Feed, and a \r for get carriage return to the most left position. This is a carryover from the computers of yesteryear, and is generally now only seen on Windows.

倒带 2024-10-02 13:20:48

\n 表示换行。这意味着光标必须转到下一行。

\r 表示回车。这意味着光标应该返回到行的开头。

Unix 程序通常只需要换行(\n)。

Windows 程序通常两者都需要。

\n means new line. It means that the cursor must go to the next line.

\r means carriage return. It means that the cursor should go back to the beginning of the line.

Unix programs usually only needs a new line (\n).

Windows programs usually need both.

向日葵 2024-10-02 13:20:48

\n 仅是换行符,\r\n 是换行符和回车符(即将光标向左移动)。

\n is a newline only, \r\n is a newline and a carriage return (i.e. move the cursor to the left).

带上头具痛哭 2024-10-02 13:20:48

不同的操作系统以不同的方式处理换行符。以下是最常见的简短列表:
DOS 和 Windows

他们期望换行符是两个字符的组合,即“\r\n”(或 13 后跟 10)。

Unix(以及 Linux)

Unix 使用单个 '\n' 来表示新行。

Mac

Mac 使用单个“\r”。

因此,当您使用文件夹路径等将应用程序从 Windows 移植到 Mac 时,这会导致问题。

Different Operating Systems handle newlines in a different way. Here is a short list of the most common ones:
DOS and Windows

They expect a newline to be the combination of two characters, namely '\r\n' (or 13 followed by 10).

Unix (and hence Linux as well)

Unix uses a single '\n' to indicate a new line.

Mac

Macs use a single '\r'.

so this causes problems when u port your app from windows to mac when u're using folder path's and alike.

往昔成烟 2024-10-02 13:20:48

'\n' 是 Unix 中的默认值,'\r\n' 是 Windows 中的默认值。

'\n' is the default in Unix, '\r\n' is the default in Windows.

标点 2024-10-02 13:20:48

这就是在 Windows (\r\n) 和 linux (\n) 操作系统中表示新行的方式。

在 Unix 上,\r 是回车符 (CR),
\na 换行符 (LF) 一起恰好是 Windows 换行符
标识符,并且您将它们替换为 Unix 换行符标识符。

在 Windows 上,\r 也是 CR,但 \n 是 CR 和 LF 的组合。所以
实际上,您正在尝试将 CR+CR+LF 替换为 CR+LF。赚不了多少钱
感觉,确实如此。

来自“perldoc perlop”:
所有系统都使用虚拟的“\n””来表示行终止符,
称为“换行符”。不存在不变的、物理的东西
换行符。操作系统只是一种幻觉,
设备驱动程序、C 库和 Perl 都共同保存。不是全部
系统将“\r”读取为 ASCII CR,将“\n”读取为 ASCII LF。例如,关于
Mac 上,这些是相反的,并且在没有行终止符的系统上,
打印“\n””可能不会发出任何实际数据。一般来说,当您
意味着您的系统的“换行符”,但当您
需要一个准确的字符。例如,大多数网络协议期望
并且更喜欢使用 CR+LF(“\015\012”或“\cM\cJ”)作为行终止符,
尽管他们经常只接受“\012”,但他们很少容忍仅仅
“”\015“”。如果您养成了使用“\n”进行网络连接的习惯,那么您
可能有一天会被烧毁。

This is how new line is represented in operating systems Windows (\r\n)and linux (\n)

On Unix the \r is a Carriage Return (CR) and the
\n a Line Feed (LF) which together happen to be the be Windows newline
identifier and you are replacing them with a Unix newline identifier.

On Windows the \r is a CR, too, but the \n is a combination of CR and LF. So
effectively you are trying to replace CR+CR+LF with CR+LF. Doesn't make much
sense, does it.

From "perldoc perlop":
All systems use the virtual ""\n"" to represent a line terminator,
called a "newline". There is no such thing as an unvarying, physical
newline character. It is only an illusion that the operating system,
device drivers, C libraries, and Perl all conspire to preserve. Not all
systems read ""\r"" as ASCII CR and ""\n"" as ASCII LF. For example, on
a Mac, these are reversed, and on systems without line terminator,
printing ""\n"" may emit no actual data. In general, use ""\n"" when you
mean a "newline" for your system, but use the literal ASCII when you
need an exact character. For example, most networking protocols expect
and prefer a CR+LF (""\015\012"" or ""\cM\cJ"") for line terminators,
and although they often accept just ""\012"", they seldom tolerate just
""\015"". If you get in the habit of using ""\n"" for networking, you
may be burned some day.

伴梦长久 2024-10-02 13:20:48

区别在于不同的操作系统之间,在 Windows 上,换行符是 \r\n 而在 Linux 上只是 \n Mac OSX 上只有 \r 这实际上只是操作系统设计者将其设计成什么样子的问题

The difference is between different operating systems, on Windows, the newline character is \r\n while on Linux it is just \n Mac OSX has just \r its really just a matter of what the designers of the OS made it to be

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文