C# 为什么 "\n" 在 Windows 上换行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10034585/
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
Why does "\n" give a new line on Windows?
提问by user1032613
The line-break marker on Windows should be CR+LFwhereas on Unix, it's just LF.
Windows 上的换行标记应该是,CR+LF而在 Unix 上,它只是LF.
So when I use something like Console.Write("line1\nline2");, why would it work "properly" and give me two lines? I expect this \nnot to work, and only a combo of \r\nwould work.
所以当我使用类似的东西时Console.Write("line1\nline2");,为什么它会“正常”工作并给我两行?我希望这\n不起作用,只有组合\r\n才能起作用。
采纳答案by Eric J.
'\n'is the Line Feed character. Traditionally, it caused the printer to roll the paper up one line. '\r'is the Carriage Return character, which traditionally caused the printer head to move to the far left edge of the paper.
'\n'是换行符。传统上,它会导致打印机将纸张卷起一行。'\r'是回车符,传统上它会导致打印头移动到纸张的最左边缘。
On printers and consoles that interpret the characters in this manner, the output of line1\nline2would be
在以这种方式解释字符的打印机和控制台上,输出line1\nline2将是
line1
line2
Many consoles (and editors) will interpret '\n' to mean that you want to start a new line andposition the cursor at the beginning of that new line. That is what you see here.
许多控制台(和编辑器)会将 '\n' 解释为意味着您要开始一个新行并将光标定位在该新行的开头。这就是你在这里看到的。
You should use Environment.NewLinerather than hard-coding any particular constants.
您应该使用Environment.NewLine而不是硬编码任何特定的常量。
回答by Jesse C. Slicer
File encodings != Consoleinterpretation.
文件编码 !=Console解释。
In other words, while the "Windows Standard" of CR+ LFexists for files, just the LF, or \nhas resulted in the appropriate carriage return and new line interpretation in console windows.
换句话说,虽然CR+的“Windows 标准”LF存在于文件中,但只有LF, 或\n在控制台窗口中导致了适当的回车和换行解释。
回答by Colin D
\n is the line feed character. On both *nix and Windows systems it should create 2 lines. The \r is the carriage return, it moves the writing instrument to the beginning of the line.
\n 是换行符。在 *nix 和 Windows 系统上,它应该创建 2 行。\r 是回车符,它将书写工具移动到行首。
Most modern consoles/editors are resilient enough to interpret \n as \r\n
大多数现代控制台/编辑器都有足够的弹性将 \n 解释为 \r\n
回答by codeblooded
In my experience, when you output to the Console with WriteLine() it accepts the \n escape character. When you are using a StreamWriter and call WriteLine() it makes you type \r\n to move to a new line. I assume that the Console has been programmed to accept the \n escape character without the carriage return \r.
根据我的经验,当您使用 WriteLine() 输出到控制台时,它接受 \n 转义字符。当您使用 StreamWriter 并调用 WriteLine() 时,它会让您键入 \r\n 以移至新行。我假设控制台已被编程为接受 \n 转义字符而没有回车 \r。
回答by David Heffernan
This is just the standard behaviour of the underlying Windows console. A native C app will do exactly the same if you output 0x0Ato the console.
这只是底层 Windows 控制台的标准行为。如果您输出0x0A到控制台,本机 C 应用程序将执行完全相同的操作。
Of course, you should be using Environment.NewLinefor your new lines. Environment.NewLineresolves to \r\non Windows and \non Unix like systems.
当然,您应该Environment.NewLine用于新线路。Environment.NewLine解析为\r\n在 Windows 和\n类 Unix 系统上。

