回车和换行...C# 中都需要吗?

发布于 2024-10-20 12:56:54 字数 437 浏览 8 评论 0原文

在将新行字符插入字符串时,我通常这样做:

str = "First line\nSecond line";

在 C# 中,这是标准做法吗?我还应该包含“回车”字符“\r”吗?以下各项之间有什么区别吗?如果有,它们是什么?

str = "First line\nSecond line";
str = "First line\r\nSecond line";

如果同时使用“回车”和“换行”是标准做法,是否有特定的顺序以及为什么?

注意:我阅读了一些关于 SO 的其他帖子,但没有找到特定于 .NET/C# 的答案。

编辑:测试了一个小应用程序后,我没有发现 '\n' 和 '\n\r' 或 '\r\n' 之间有任何区别。

When inserting a new line character into a string I have usually done this:

str = "First line\nSecond line";

In C#, is this the standard practice? Should I also include the 'carriage return' character '\r'? Are there any difference between the following, and if so, what are they?

str = "First line\nSecond line";
str = "First line\r\nSecond line";

If using both 'carriage return' and 'line feed' is standard practice, is there a specific order and why?

Note: I read a few other posts on SO but didn't find an answer specific to .NET/C#.

Edit: After testing a little app, I didn't not see any difference between '\n' and '\n\r' or '\r\n'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不知所踪 2024-10-27 12:56:54

System.Environment.NewLine 是您正在寻找的常量 - http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx 它将提供特定于环境的组合,给定操作系统上的大多数程序都会考虑“下一行”文本”。

实际上,大多数文本工具都将包含 \n 的所有变体视为“换行”,您可以在文本 “foo\nbar” 中使用它。特别是如果您尝试构造多行格式字符串,例如 $"V1 = {value1}\nV2 = {value2}\n"。如果您正在使用字符串连接构建文本,请考虑使用 NewLine。在任何情况下,请确保您使用的工具以您想要的方式理解输出,并且您可能需要例如始终使用 \r\n,无论平台如何,如果您选择的编辑器可以否则无法正确打开文件。

请注意,WriteLine 方法使用 NewLine,因此,如果您打算使用这些方法编写文本,请避免仅使用 \n,因为结果文本可能包含以下内容的混合: \r\n\n 可能会混淆一些工具,而且看起来绝对不整洁。

有关历史背景,请参阅 \n 和 \r 之间的区别?

System.Environment.NewLine is the constant you are looking for - http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx which will provide environment specific combination that most programs on given OS will consider "next line of text".

In practice most of the text tools treat all variations that include \n as "new line" and you can just use it in your text "foo\nbar". Especially if you are trying to construct multi-line format strings like $"V1 = {value1}\nV2 = {value2}\n". If you are building text with string concatenation consider using NewLine. In any case make sure tools you are using understand output the way you want and you may need for example always use \r\n irrespective of platform if editor of your choice can't correctly open files otherwise.

Note that WriteLine methods use NewLine so if you plan to write text with one these methods avoid using just \n as resulting text may contain mix of \r\n and just \n which may confuse some tools and definitely does not look neat.

For historical background see Difference between \n and \r?

芯好空 2024-10-27 12:56:54

回车符 \r 将光标移动到当前行的开头。换行符 \n 会导致跳转到下一行,并且可能到下一行的开头;这是 Alexei 上面提到的依赖于平台的部分(在 *nix 系统上 \n 为您提供回车符和换行符,在 Windows 中则没有)

您使用的内容取决于您的用途试图做。例如,如果我想在控制台上制作一些旋转的东西,我会这样做 str = "|\r/\r-\r\\\r";

A carriage return \r moves the cursor to the beginning of the current line. A newline \n causes a drop to the next line and possibly the beginning of the next line; That's the platform dependent part that Alexei notes above (on a *nix system \n gives you both a carriage return and a newline, in windows it doesn't)

What you use depends on what you're trying to do. If I wanted to make a little spinning thing on a console I would do str = "|\r/\r-\r\\\r"; for example.

段念尘 2024-10-27 12:56:54

这取决于您显示文本的位置。例如,在控制台或文本框中,\n 就足够了。在 RichTextBox 上,我认为两者都需要。

It depends on where you're displaying the text. On the console or a textbox for example, \n will suffice. On a RichTextBox I think you need both.

老娘不死你永远是小三 2024-10-27 12:56:54

我知道这有点旧,但是对于任何偶然发现此页面的人都应该知道 \n 和 \r\n 之间有区别。

\r\n 给出 CRLF 行结束符,\n 给出 LF 行结束符。一般来说,肉眼看没有什么区别。

从字符串创建一个 .txt,然后尝试在记事本中打开(正常不是记事本++),您会注意到差异

SHA,PCT,PRACTICE,BNF CODE,BNF NAME,ITEMS,NIC,ACT COST,QUANTITY,PERIOD
Q44,01C,N81002,0101021B0AAALAL,Sod Algin/Pot Bicarb_Susp S/F,3,20.48,19.05,2000,201901
Q44,01C,N81002,0101021B0AAAPAP,Sod Alginate/Pot Bicarb_Tab Chble 500mg,1,3.07,2.86,60,201901

上面使用的是“CRLF”,下面是“仅 LF”的样子(有一个字符在 LF 显示的地方看不到)。

SHA,PCT,PRACTICE,BNF CODE,BNF NAME,ITEMS,NIC,ACT COST,QUANTITY,PERIODQ44,01C,N81002,0101021B0AAALAL,Sod Algin/Pot Bicarb_Susp S/F,3,20.48,19.05,2000,201901Q44,01C,N81002,0101021B0AAAPAP,Sod Alginate/Pot Bicarb_Tab Chble 500mg,1,3.07,2.86,60,201901

如果需要更正行尾并且文件足够小,您可以在 NotePad++ 中更改行尾(或粘贴到 word 中然后再回到记事本中 - 尽管这只会生成 CRLF)。

这可能会导致读取这些文件的某些功能不再起作用(给出的示例行来自 GP 处方数据 - 英格兰。文件已从 CRLF 行结束更改为 LF 行结束)。这会阻止 SSIS 作业运行并失败,因为无法读取 LF 行结尾。

行结束信息来源:
https://en.wikipedia.org/wiki/Newline#Representations_in_ Different_character_encoding_specifications

希望这有帮助将来的某个人:) CRLF = 基于 Windows,LF 或 CF 来自基于 Unix 的系统(Linux、MacOS 等)

I know this is a little old, but for anyone stumbling across this page should know there is a difference between \n and \r\n.

The \r\n gives a CRLF end of line and the \n gives an LF end of line character. There is very little difference to the eye in general.

Create a .txt from the string and then try and open in notepad (normal not notepad++) and you will notice the difference

SHA,PCT,PRACTICE,BNF CODE,BNF NAME,ITEMS,NIC,ACT COST,QUANTITY,PERIOD
Q44,01C,N81002,0101021B0AAALAL,Sod Algin/Pot Bicarb_Susp S/F,3,20.48,19.05,2000,201901
Q44,01C,N81002,0101021B0AAAPAP,Sod Alginate/Pot Bicarb_Tab Chble 500mg,1,3.07,2.86,60,201901

The above is using 'CRLF' and the below is what 'LF only' would look like (There is a character that cant be seen where the LF shows).

SHA,PCT,PRACTICE,BNF CODE,BNF NAME,ITEMS,NIC,ACT COST,QUANTITY,PERIODQ44,01C,N81002,0101021B0AAALAL,Sod Algin/Pot Bicarb_Susp S/F,3,20.48,19.05,2000,201901Q44,01C,N81002,0101021B0AAAPAP,Sod Alginate/Pot Bicarb_Tab Chble 500mg,1,3.07,2.86,60,201901

If the Line Ends need to be corrected and the file is small enough in size, you can change the line endings in NotePad++ (or paste into word then back into Notepad - although this will make CRLF only).

This may cause some functions that read these files to potenitially no longer function (The example lines given are from GP Prescribing data - England. The file has changed from a CRLF Line end to an LF line end). This stopped an SSIS job from running and failed as couldn't read the LF line endings.

Source of Line Ending Information:
https://en.wikipedia.org/wiki/Newline#Representations_in_different_character_encoding_specifications

Hope this helps someone in future :) CRLF = Windows based, LF or CF are from Unix based systems (Linux, MacOS etc.)

奢望 2024-10-27 12:56:54

这始终是一个好主意,虽然并不总是必需的,但 Windows 标准将两者都包括在内。

\n 实际上代表一个换行,或者数字 10,并且规范地换行意味着在终端和电传打字机上“向下移动一行”。

\r 代表 CR、回车符或数字 13。在 Windows、Unix 和大多数终端上,CR 将光标移动到行首。 (8 位计算机的情况并非如此:大多数计算机都会使用 CR 前进到下一行。)

无论如何,某些进程(例如文本控制台)可能会在您发送 LF 时自动添加 CR。然而,由于 CR 只是移动到行的开头,因此发送 CR 两次也没有什么坏处。

另一方面,对话框、文本框和其他显示元素需要同时使用 CR 和 LF 才能正确开始新行。

由于同时发送两者实际上没有任何缺点,并且在某些情况下两者都是必需的,因此最简单的策略是如果您不确定,则同时使用两者。

It's always a good idea, and while it's not always required, the Windows standard is to include both.

\n actually represents a Line Feed, or the number 10, and canonically a Line Feed means just "move down one row" on terminals and teletypes.

\r represents CR, a Carriage Return, or the number 13. On Windows, Unix, and most terminals, a CR moves the cursor to the beginning of the line. (This is not the case for 8-bit computers: most of those do advance to the next line with a CR.)

Anyway, some processes, such as the text console, might add a CR automatically when you send an LF. However, since the CR simply moves to the start of the line, there's no harm in sending the CR twice.

On the other hand, dialog boxes, text boxes, and other display elements require both CR and LF to properly start a new line.

Since there's really no downside to sending both, and both are required in some situations, the simplest policy is to use both, if you're not sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文