.net 提醒 - \r\n 还是 \n\r?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6539801/
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-09-03 15:41:39  来源:igfitidea点击:

Reminder - \r\n or \n\r?

.netwindowsascii

提问by Daniel Mo?mondor

I just can't remember those. So, what is the right way to properly terminate old fashioned ASCII lines?

我就是想不起来那些。那么,正确终止老式 ASCII 行的正确方法是什么?

回答by David Snabel-Caunt

I'd use the word 'return' to remember, the r comes before the n.

我会使用“ retur n”这个词来记住,r 在 n 之前。

回答by BrunoLM

If you are using C# you should use Environment.NewLine, which accordingly to MSDN it is:

如果您使用的是 C#,则应该使用Environment.NewLine,根据 MSDN,它是:

A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.

包含“\r\n”的字符串用于非 Unix 平台,或包含“\n”的字符串用于 Unix 平台。

回答by Sun

New line depends on your OS:

新行取决于您的操作系统:

DOS & Windows: \r\n 0D0A (hex), 13,10 (decimal)
Unix & Mac OS X: \n, 0A, 10
Macintosh (OS 9): \r, 0D, 13

More details here: https://ccrma.stanford.edu/~craig/utility/flip/

更多细节在这里:https: //ccrma.stanford.edu/~craig/utility/flip/

When in doubt, use any freeware hex viewer/editor to see how a file encodes its new line.

如有疑问,请使用任何免费软件十六进制查看器/编辑器查看文件如何对其新行进行编码。

For me, I use following guide to help me remember: 0D0A = \r\n = CR,LF = carriage return, line feed

对于我来说,我使用下面的指南以帮助自己记住:0D0A = \ r \ n = CR,LF = CA让rriage [RE打开,李ñË饲料

回答by Otávio Décio

The sequence is CR (Carriage Return) - LF (Line Feed). Remember dot matrix printers? Exactly. So - the correct order is \r \n

顺序是 CR(回车)- LF(换行)。还记得点阵打印机吗?确切地。所以 - 正确的顺序是 \r \n

回答by Esteban Araya

In any .NET langauge, Environment.NewLinewould be preferable.

在任何 .NET 语言中,Environment.NewLine都会更可取。

回答by Martin York

Be careful with doing this manually.
In fact I would advise notdoing this at all.

手动执行此操作时要小心。
事实上,我建议根本不要这样做。

In reality we are talking about the line termination sequence LTS that is specific to platform.

实际上,我们讨论的是特定于平台的线路终止序列 LTS。

If you open a file in text mode (ie not binary) then the streams will convert the "\n" into the correct LTS for your platform. Then convert the LTS back to "\n" when you read the file.

如果您以文本模式(即非二进制)打开文件,则流会将“\n”转换为适合您平台的正确 LTS。然后在读取文件时将 LTS 转换回 "\n"。

As a result if you print "\r\n" to a windows file you will get the sequence "\r\r\n" in the physical file (have a look with a hex editor).

因此,如果您将 "\r\n" 打印到 Windows 文件,您将在物理文件中获得序列 "\r\r\n" (使用十六进制编辑器查看)。

Of course this is real pain when it comes to transferring files between platforms.

当然,在平台之间传输文件时,这真的很痛苦。

Now if you are writing to a network stream then I would do this manually (as most network protocols call this out specifically). But I would make sure the stream is not doing any interpretation (so binary mode were appropriate).

现在,如果您正在写入网络流,那么我将手动执行此操作(因为大多数网络协议专门对此进行了调用)。但我会确保流没有做任何解释(所以二进制模式是合适的)。

回答by bhamby

From Wikipedia(you can read which is correct for your OS at that article):

维基百科(您可以在该文章中阅读适合您的操作系统的内容):

Systems based on ASCII or a compatible character set use either LF (Line feed, '\n', 0x0A, 10 in decimal) or CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, '\r\n', 0x0D0A).

基于 ASCII 或兼容字符集的系统分别使用 LF(换行、'\n'、0x0A、十进制 10)或 CR(回车、'\r'、0x0D、十进制 13),或 CR 后跟LF(CR+LF,'\r\n',0x0D0A)。

回答by Brad

\r\nfor Windows will do just fine.

\r\n对于 Windows 就可以了。

回答by Hastur

\r\n

\r\n

Odd to say I remember it because it is the opposite of the typewriterI used.
Well if it was normal I had no need to remember it... :-)

奇怪的是我记得它,因为它与我使用的打字机相反
好吧,如果这是正常的,我不需要记住它......:-)

typewriter from wikipedia*Image from Wikipedia

来自维基百科的打字机*图片来自维基百科

In the typewriter when you finish to digit the line you use the carriage return lever, that before makes roll the drum, the newline, and after allow you to manually operate the carriage return.

在打字机中,当您完成数字线时您使用回车杆,即在滚鼓之前、换行和之后允许您手动操作回车

You can listen from this record from freesound.orgthe sound of the paper feeding in the beginning, and at around -1:03 seconds from the end, after the bell warning for the end of the line sound of the drum that rolls and after the one of the carriage return.

您可以从 freesound.org这张唱片中聆听开始时的进纸声音,以及结束时的 -1:03 秒左右,在鼓声结束时的铃声警告之后以及之后回车符之一。

回答by NirMH

if you are using C#, why not using Environment.NewLine? (i assume you use some file writer objects... just pass it the Environment.NewLineand it will handle the right terminators.

如果您使用的是 C#,为什么不使用Environment.NewLine?(我假设您使用了一些文件编写器对象......只需将它传递给它Environment.NewLine,它就会处理正确的终止符。