添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

DOS/Windows newline is two (2) bytes, 0x0D0A. UNIX/Linux newline is one (1) byte, 0x0A. Mac newline is one (1) byte, 0x0D.

How can I write code to find the length of a newline on each platform? Output on Windows will produce two (2) bytes, but output on Linux will produce one (1) byte. I did not find anything related to this in about_Special_Characters .

On Windows:

PS C:\src\t> type .\nl.ps1
'`n'.Length
"`n".Length

Both Windows and Linux return the same result.

PS C:\src\t> .\nl.ps1
                Escapes aren't processed in a string literal (single quotes), which is why you see 2 for the length of the first.
                    – TheIncorrigible1
                Jul 30 '18 at 23:30
                Length property of string will only show number of characters, but not bytes. Actual number of bytes will depend on encoding you use. For example, on Windows with UTF-16 encoding newline will take 4 bytes.
                    – PetSerAl
                Jul 31 '18 at 0:30
                As an aside when classic Mac OS became OS X in 2001, it acquired a Unix foundation, and ever since then macOS (as it is now known) has used LF-only (0xA) linefeeds  (not obsolete CR-only (0xD)) linefeeds, just like other Unix-like platforms.
                    – mklement0
                Jul 31 '18 at 1:37
                And on Windows the "newline character" is not 2 bytes.  A "new line" is signified by a sequence of two characters instead of one. But "`n".length=1 on both platforms.
                    – David Browne - Microsoft
                Jul 31 '18 at 11:48

.NET Standard has an API for that, and is a always available from Powershell:

[system.environment]::newline.length

On Windows

PS> [system.environment]::newline.length

On Ubuntu

PS> [system.environment]::newline.length      
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.9.26.35031