string \n 和 \r\n 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3821784/
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
What's the difference between \n and \r\n?
提问by Emanuil Rusev
They both mean "new line" but when is one used over the other?
它们都意味着“新行”,但是什么时候使用一个?
回答by Svisstack
\r\n
is a WindowsStyle
\r\n
是Windows样式
\n
is a POSIXStyle
\n
是POSIX风格
\r
is a old pre-OS X MacsStyle, Modern Mac'susing POSIXStyle.
\r
是旧的 OS X 之前的 Mac风格,现代 Mac使用POSIX风格。
\r
is a carrige return and \n
is a line feed, in old computerswhere it not have monitor, have only printer to get out programs result to user, if you want get printing from staring of new line from left, you must get \n
for Line Feed, and \r
for get Carriage returnto the most left position, this is from old computers syntax saved to this time on Windowsplatform.
\r
是回车,\n
是换行,在没有显示器的旧计算机中,只有打印机才能将程序结果输出给用户,如果您想从左侧的新行开始打印,则必须获取\n
换行,而\r
对于 get Carriage 返回到最左边的位置,这是从Windows平台上保存到现在的旧计算机语法。
回答by Philippe Carriere
\n means new line. It means that the cursor must go to the next line.
\n 表示换行。这意味着光标必须转到下一行。
\r means carriage return. It means that the cursor should go back to the beginning of the line.
\r 表示回车。这意味着光标应该回到行首。
Unix and Unix programs usually only needs a new line (\n).
Unix 和 Unix 程序通常只需要一个新行 (\n)。
Windows and Windows programs usually need both.
Windows 和 Windows 程序通常两者都需要。
回答by Hymanson Pope
\n is a newline only, \r\n is a newline and a carriage return (i.e. move the cursor to the left).
\n 只是换行符,\r\n 是换行符和回车符(即向左移动光标)。
回答by Emerion
Different Operating Systems handle newlines in a different way. Here is a short list of the most common ones: DOS and Windows
不同的操作系统以不同的方式处理换行符。以下是最常见的简短列表:DOS 和 Windows
They expect a newline to be the combination of two characters, namely '\r\n' (or 13 followed by 10).
他们期望换行符是两个字符的组合,即 '\r\n' (或 13 后跟 10)。
Unix (and hence Linux as well)
Unix(因此也是 Linux)
Unix uses a single '\n' to indicate a new line.
Unix 使用单个 '\n' 来表示一个新行。
Mac
苹果电脑
Macs use a single '\r'.
Mac 使用单个“\r”。
so this causes problems when u port your app from windows to mac when u're using folder path's and alike.
因此,当您在使用文件夹路径等时将应用程序从 Windows 移植到 Mac 时,这会导致问题。
回答by Andriy Sholokh
This is how new line is represented in operating systems Windows (\r\n)and linux (\n)
这是在操作系统 Windows (\r\n) 和 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.
在 Unix 上,\r 是回车符 (CR) 和 \na 换行符 (LF),它们一起恰好是 Windows 换行符标识符,您将用 Unix 换行符标识符替换它们。
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.
在 Windows 上,\r 也是 CR,但 \n 是 CR 和 LF 的组合。如此有效地您正在尝试用 CR+LF 替换 CR+CR+LF。没有多大意义,对吧。
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.
来自“perldoc perlop”:所有系统都使用虚拟的“\n”来表示行终止符,称为“换行符”。没有不变的物理换行符这样的东西。操作系统、设备驱动程序、C 库和 Perl 都合力保存,这只是一种错觉。并非所有系统都将 ""\r"" 读为 ASCII CR,将 ""\n"" 读为 ASCII LF。例如,在 Mac 上,这些是相反的,在没有行终止符的系统上,打印 ""\n"" 可能不会发出实际数据。通常,当您表示系统的“换行符”时使用 ""\n"",但在需要精确字符时使用文字 ASCII。例如,大多数网络协议期望并更喜欢 CR+LF(""\015\012"" 或 "" \cM\cJ"") 用于行终止符,尽管它们通常只接受 ""\012"",但它们很少只接受 ""\015""。如果你养成了使用 ""\n"" 网络的习惯,你可能有一天会被烧死。
回答by Thomas Mueller
'\n' is the default in Unix, '\r\n' is the default in Windows.
'\n' 是 Unix 中的默认值,'\r\n' 是 Windows 中的默认值。
回答by Rezler
In C#, you can just use Environment.NewLine
在 C# 中,你可以只使用 Environment.NewLine
回答by Richard J. Ross III
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
区别在于不同的操作系统之间,在 Windows 上,换行符是\r\n
在 Linux 上,而在\n
Mac OSX 上,\r
它实际上只是操作系统设计者所做的事情