How can I get the number of bytes in a newline from PowerShell?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

How can I get the number of bytes in a newline from PowerShell?



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.


about_Special_Characters



On Windows:


PS C:srct> type .nl.ps1
'`n'
'`n'.Length
"`n"
"`n".Length



Both Windows and Linux return the same result.


PS C:srct> .nl.ps1
`n
2


1




1 Answer
1



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


[system.environment]::newline.length



On Windows


PS C:Usersdbrowne> [system.environment]::newline.length
2



On Ubuntu


PS /home/dbrowne> [system.environment]::newline.length
1






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Mass disable jenkins jobs