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

2019-01-22 11:59发布

As per subject...

9条回答
男人必须洒脱
2楼-- · 2019-01-22 12:28

\n is the line break used by Unix(-like) systems, \r\n is used by windows. This has nothing to do with C#.

查看更多
Evening l夕情丶
3楼-- · 2019-01-22 12:31

Basically comes down to Windows standard: \r\n and Unix based systems using: \n

http://en.wikipedia.org/wiki/Newline

查看更多
小情绪 Triste *
4楼-- · 2019-01-22 12:36

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

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

\r\n = CR + LF // Used as a new line character in 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 in Windows, and you use CR + LF ("\n\r" instead of Environment.NewLine) as new line character in your strings, and then MS 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 operating system.

查看更多
Lonely孤独者°
5楼-- · 2019-01-22 12:37

It's about how operating system recognize 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).

查看更多
Anthone
6楼-- · 2019-01-22 12:42

"\n" is just a line feed (Unicode U+000A). This is typically the Unix line separator.

"\r\n" is a carriage return (Unicode U+000D) followed by a line feed (Unicode U+000A). This is typically the Windows line separator.

查看更多
何必那么认真
7楼-- · 2019-01-22 12:43

\n is Unix, \r is Mac, \r\n is Windows.

Sometimes it's giving trouble especially when running code cross platform. You can bypass this by using Environment.NewLine.

Please refer to What is the difference between \r, \n and \r\n ?! for more information. Happy reading

查看更多
登录 后发表回答