连接字符串时遇到格式问题

Encounter format trouble in concatenating String

文件格式类似于

abc def 
    ghy jk
          lmp

这是我的代码

StreamReader file = new StreamReader("./test.txt");
string afterreplace = "";
while ((line = file.ReadLine()) != null)
{
    Console.WriteLine(line);
    afterreplace = String.Concat(afterreplace, line); 
}
Console.WriteLine(afterreplace);

但是我在输出中得到了这个

与原文件不符

abc def     ghy jk      lmp

因为我可以添加一些换行符,我真的很想知道为什么会导致

String.Concat() 会忽略 \n 吗?我怎样才能达到我的期望?

不是 string.Concat 这样做,而是 file.ReadLine()。但是您没有直接在输出中注意到这一点,因为 Console.WriteLine 添加了一个额外的换行符。

来自 StreamReader.ReadLine(强调我的):

Remarks

A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed. [...]