输出字符串,在控制台应用程序中包含换行符

Hen*_*alg 2 .net c# console-application

我正在处理我正在处理的控制台应用程序的输出数据中的换行符时遇到问题.下面的简化示例是将outDataString写入out.txt而不使用换行符.在这种情况下,有关如何在out.txt中实现换行的任何想法?

FileStream ostrm;
StreamWriter writer;

string outDataString = "Hi, this is the out data.\nWith a few\n line breaks.";

ostrm = new FileStream("out.txt", FileMode.OpenOrCreate, FileAccess.Write);                
writer = new StreamWriter(outDataString);

Console.SetOut(writer);
Console.write(outDataString);
Run Code Online (Sandbox Code Playgroud)

谢谢.

Ali*_*eza 5

string outDataString = string.Format(
"Hi, this is the out data.{0}With a few{0} line breaks.",
Environment.NewLine);
Run Code Online (Sandbox Code Playgroud)

  • @PanagiotisKanavos这是不成熟的优化.从最正确的*开始,这是使用Environment.NewLine,然后如果(如果!)存在性能问题,您可以查看替代方案. (4认同)
  • @Panagiotis它更具可读性,并且没有太多性能问题需要担心 (2认同)