string \r\n、\r 和\n 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15433188/
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
\r\n, \r and \n what is the difference between them?
提问by Haya Raed
I need to ask about the difference in a string between \r\n
, \r
and \n
. How is a string affected by each?
我需要询问\r\n
,\r
和之间字符串的区别\n
。每个字符串如何影响字符串?
I have to replace the occurrences of \r\n
and \r
with \n
but I cannot get how are they different in a string...
我不得不更换的出现\r\n
,并\r
用\n
,但我不能让他们如何在一个字符串不同...
I know that \r
is like hitting enter and \n
is for a new line.
我知道这\r
就像按回车键并且 \n
是换行。
回答by Tom Bowen
\r
= CR (Carriage Return) → Used as a new line character in Mac OS before X\n
= LF (Line Feed) → Used as a new line character in Unix/Mac OS X\r\n
= CR + LF → Used as a new line character in Windows
\r
= CR(回车)→ X 之前在 Mac OS 中用作换行符\n
= LF(换行)→ 在 Unix/Mac OS X 中用作换行符\r\n
= CR + LF → 在 Windows 中用作换行符
回答by Exasm
All 3 of them represent the end of a line. But...
他们三个都代表一行的结尾。但...
\r
(Carriage Return)→ moves the cursor to the beginning of the line without advancing to the next line\n
(Line Feed)→ moves the cursor down to the next line without returning to the beginning of the line — In a *nix environment\n
moves to the beginning of the line.\r\n
(End Of Line)→ a combination of\r
and\n
\r
(回车)→ 将光标移动到行首而不前进到下一行\n
(换行)→ 将光标向下移动到下一行而不返回到行首——在 *nix 环境中\n
移动到行首。\r\n
(行尾)→的组合\r
和\n
回答by durilka
They are normal symbols as 'a' or 'ю' or any other. Just (invisible) entries in a string. \r moves cursor to the beginning of the line. \n goes one line down.
它们是普通符号,如“a”或“ю”或任何其他符号。只是(不可见)字符串中的条目。\r 将光标移动到行首。\n 向下一行。
As for your replacement, you haven't specified what language you're using, so here's the sketch:
至于你的替代品,你还没有指定你使用的是什么语言,所以这是草图:
someString.replace("\r\n", "\n").replace("\r", "\n")
someString.replace("\r\n", "\n").replace("\r", "\n")
回答by Matten
A carriage return (\r
) makes the cursor jump to the first column (begin of the line) while the newline (\n
) jumps to the next line and eventuallyto the beginning of that line. So to be sure to be at the first position within the next line one uses both.
回车 ( \r
) 使光标跳到第一列(行首),而换行符 ( \n
) 跳到下一行并最终跳到该行的开头。所以为了确保在下一行的第一个位置,一个使用两者。