powershell - 如何从 PowerShell 获取换行符中的字节数?

标签 powershell powershell-v6.0

DOS/Windows 换行符是两 (2) 个字节,0x0D0A。 UNIX/Linux 换行符是一 (1) 个字节,0x0A。 Mac 换行符是一 (1) 个字节,0x0D。

如何编写代码来查找每个平台上换行符的长度? Windows 上的输出将产生两 (2) 个字节,但 Linux 上的输出将产生一 (1) 个字节。我在 about_Special_Characters 中没有找到与此相关的任何内容。

在 Windows 上:

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

Windows 和 Linux 返回相同的结果。

PS C:\src\t> .\nl.ps1
`n
2


1

最佳答案

.NET Standard有一个用于此目的的 API,并且始终可以从 Powershell 获取:

[system.environment]::newline.length

在 Windows 上

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

在 Ubuntu 上

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

关于powershell - 如何从 PowerShell 获取换行符中的字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51603409/

相关文章:

powershell - 读取重启日志时出错

powershell - 在ForEach-Object中使用PowerShell -is运算符时始终匹配PSCustomObject

powershell - 查找阵列中的所有IP地址

azure - 无法将 Azure 自动化中的 AZAccount 与服务主体连接

powershell - 比较新的时间跨度总时间..未按预期运行

arrays - Powershell根据属性值比较2个哈希表数组

git - jekyll 服务器已打开但未重新生成

powershell - 如何从另一个 PS session 启动一个新的 PowerShell session ,并运行带有 splatted 参数的 CmdLet

powershell - 使用 -PathType 参数如何影响 Test-Path 的效率?

powershell - 如何解决错误[术语 'pwsh.exe' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称]?