如何在 PowerShell 字符串中对 Unicode 字符 U+0048 (H) 进行编码?

在 C# 中我会这样做: "\u0048", ,但这似乎在 PowerShell 中不起作用。

有帮助吗?

解决方案

替换 '\ U' 与加 '0x',并将其转换为System.Char:

PS > [char]0x0048
H

您还可以使用“$()”的语法中嵌入Unicode字符为一个字符串:

PS > "Acme$([char]0x2122) Company"
AcmeT Company

其中,T是用于非注册商标字符的Powershell的表示。

其他提示

根据文档,PowerShell Core 6.0 添加了对此转义序列的支持:

PS> "`u{0048}"
H

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-6#unicode-character-ux

也许这不是PowerShell的方式,但是这是我做的。我发现它是清洁器。

[regex]::Unescape("\u0048") # Prints H
[regex]::Unescape("\u0048ello") # Prints Hello
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top