In C#, what's the difference between \n and \r\n?

后端 未结 8 1262
礼貌的吻别
礼貌的吻别 2020-12-14 00:37

As per subject...

8条回答
  •  执笔经年
    2020-12-14 01:06

    \n = LF (Line Feed) // Used as a new line character on Unix

    \r = CR (Carriage Return) // Used as a new line character on Mac

    \r\n = CR + LF // Used as a new line character on Windows

    (char)13 = \r = CR

    Environment.NewLine = any of the above code based on the operating system

    // .NET provides the Environment class which provides many data based on operating systems, so if the application is built on Windows, and you use CR + LF ("\n\r" instead of Environment.NewLine) as the new line character in your strings, and then Microsoft creates a VM for running .NET applications in Unix, then there will be problem. So, you should always use Environment.NewLine when you want a new line character. Now you need not to care about the operating system.

提交回复
热议问题