C# 插入 LineFeed 而不是 CRLF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11954929/
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
Insert LineFeed instead of CRLF
提问by Bohn
Using StringBuilder and in my string I am using Environment.NewLine, when I open it it shows as CRLF, Is there another commands in C# that the output shows as "LF" only and not "CRLF"?
使用 StringBuilder 并且在我的字符串中我使用 Environment.NewLine,当我打开它时它显示为 CRLF,C# 中是否有另一个命令输出仅显示为“LF”而不是“CRLF”?
采纳答案by Olivier Jacot-Descombes
Simply write
简单地写
sb.Append((char)10);
or more readable
或更易读
sb.Append('\n');
even more readable
更具可读性
const char LF = '\n';
sb.Append(LF);
回答by mathieu
Use escaped character '\n' instead of Environment.Newline
使用转义字符 '\n' 而不是 Environment.Newline
See http://msdn.microsoft.com/en-us/library/h21280bw.aspxfor escaped characters list and http://msdn.microsoft.com/en-us/library/system.environment.newline.aspxfor Environment.Newline behavior.
请参阅http://msdn.microsoft.com/en-us/library/h21280bw.aspx了解转义字符列表和http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx了解环境.换行行为。
回答by CodeCaster
The Environment.NewLineexists solely to differ between Windows-like line endings (\r\n) and Unix-style line endings (\n), so when writing text files and the like you don't have to bother which one to use (imagine you're running on Mono on Linux, then you want just \n, which the Environment. NewLinewill contain as it is set by the runtime).
该Environment.NewLine只之间存在着不同的类似Windows的行尾(\r\n)和Unix风格的行尾(\n),所以写文本文件时,和像你不必费心使用哪一个(想象一下,你在单声道运行Linux,那么您只需要\n,Environment. NewLine它将包含运行时设置的内容)。
So when you know you always and only want a line feed character, simply put \nin your code. It won't change.
所以当你知道你总是并且只想要一个换行符时,只需\n输入你的代码。它不会改变。

