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

后端 未结 8 1259
礼貌的吻别
礼貌的吻别 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.

    0 讨论(0)
  • 2020-12-14 01:08

    It's about how the operating system recognizes line ends.

    • Windows user \r\n
    • Mac user \r
    • Linux uses \n

    Morale: if you are developing for Windows, stick to \r\n. Or even better, use C# string functions to deal with strings which already consider line endings (WriteLine, and such).

    0 讨论(0)
提交回复
热议问题