\n(换行符)和\r(回车符)有什么区别?

特别地,\n和\r之间有什么实际的区别吗?是否有应该使用其中一种而不是另一种的地方?


当前回答

更令人困惑的是,我一直在使用浏览器中的HTML页面中的TextArea元素编写一个简单的文本编辑器。考虑到CR/LF的兼容性问题,我编写了代码来检查平台,并使用适用于平台的换行符约定。

然而,当检查TextArea中包含的实际字符时,我发现了一些有趣的事情,通过一个小的JavaScript函数生成对应于字符的十六进制数据。

为了测试,我输入了以下文本:

你好,世界(输入)

再见,残酷的世界[进入]

当我检查文本数据时,得到的字节序列是这样的:

48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 0a 47 6f 6f 64 62 79 65 2c 20 43 72 75 65 6c 20 57 6f 72 6c 64 0a

现在,大多数人看到这个,看到0a而没有0d字节,会认为这个输出是在Unix/Linux平台上获得的。但是,这里有一个问题:我在Windows 7 64位的谷歌Chrome中获得的这个序列。

因此,如果你正在使用一个TextArea元素并检查文本,检查输出,就像我上面所做的那样,以确保从你的TextArea返回的实际字符字节。我还没有看到这在其他平台或其他浏览器上是否有所不同,但如果您通过JavaScript执行文本处理,并且需要使该文本处理平台独立,那么就值得记住这一点。

上述文章中涉及的约定适用于控制台输出,但HTML元素似乎遵循UNIX/Linux约定。除非有人在不同的平台/浏览器上发现了其他情况。

其他回答

在ascii码方面,它是3——因为它们分别是10和13;-)。

但说真的,有很多:

in Unix and all Unix-like systems, \n is the code for end-of-line, \r means nothing special as a consequence, in C and most languages that somehow copy it (even remotely), \n is the standard escape sequence for end of line (translated to/from OS-specific sequences as needed) in old Mac systems (pre-OS X), \r was the code for end-of-line instead in Windows (and many old OSs), the code for end of line is 2 characters, \r\n, in this order as a (surprising;-) consequence (harking back to OSs much older than Windows), \r\n is the standard line-termination for text formats on the Internet for electromechanical teletype-like "terminals", \r commands the carriage to go back leftwards until it hits the leftmost stop (a slow operation), \n commands the roller to roll up one line (a much faster operation) -- that's the reason you always have \r before \n, so that the roller can move while the carriage is still going leftwards!-) Wikipedia has a more detailed explanation. for character-mode terminals (typically emulating even-older printing ones as above), in raw mode, \r and \n act similarly (except both in terms of the cursor, as there is no carriage or roller;-)

In practice, in the modern context of writing to a text file, you should always use \n (the underlying runtime will translate that if you're on a weird OS, e.g., Windows;-). The only reason to use \r is if you're writing to a character terminal (or more likely a "console window" emulating it) and want the next line you write to overwrite the last one you just wrote (sometimes used for goofy "ascii animation" effects of e.g. progress bars) -- this is getting pretty obsolete in a world of GUIs, though;-).

两个不同的角色。

\n在Unix文本文件中用作行结束符

\r历史上(在os X之前)在Mac文本文件中用作行结束符

\r\n(即同时使用)用于终止Windows和DOS文本文件中的行。

才能完成,

在shell (bash)脚本中,您可以使用\r发送光标,在行前,当然,\n将光标放在新行上。

例如,试着:

echo -en "AA--AA" ; echo -en "BB" ; echo -en "\rBB"

第一个“回声”显示AA——AA 第二:AA—AABB 最后一个:BB—AABB

但是不要忘记使用-en作为参数。

In windows, the \n moves to the beginning of the next line. The \r moves to the beginning of the current line, without moving to the next line. I have used \r in my own console apps where I am testing out some code and I don't want to see text scrolling up my screen, so rather than use \n after printing out some text, of say, a frame rate (FPS), I will printf("%-10d\r", fps); This will return the cursor to the beginning of the line without moving down to the next line and allow me to have other information on the screen that doesn't get scrolled off while the framerate constantly updates on the same line (the %-10 makes certain the output is at least 10 characters, left justified so it ends up padded by spaces, overwriting any old values for that line). It's quite handy for stuff like this, usually when I have debugging stuff output to my console screen.

一点历史

/r代表返回或回车,它的历史归功于打字机。一个回车符将你的回车符一直向右移动,这样你就在行首输入了。

/n代表新行,同样,从打字的日子开始,你移动到新的行。但不一定要在开头,这就是为什么一些操作系统同时需要/r返回和/n换行符,因为打字机的换行顺序是这样的。这也解释了旧的8位计算机使用Return而不是Enter,从回车中返回,这是很熟悉的。

因为没有人特别提到它(他们是不是太小了,不知道/不记得?)-我怀疑\r\n的使用起源于打字机和类似的设备。

当你在使用多行打字机时,想要打出新的一行,它必须执行两个物理操作:将滑架滑回页面的开头(在美国是左),并将纸张上一个缺口。

在行式打印机的时代,打印粗体文本的唯一方法,例如,是做一个没有换行符的回车,并在旧的字符上打印相同的字符,从而添加更多的墨水,从而使它看起来更暗(粗体)。当打字机的机械式“换行”功能失效时,令人讨厌的结果是:如果你不注意,你可能会把上一行文本敲过。