首页 > 解决方案 > 字符串中的Powershell unicode字符

问题描述

我已阅读 Microsoft powershell 官方文档,并找到了以下示例:

"`u{0048}"

它应该显示一个 H

但是,就我而言,它显示:

你{0048}

我不需要替代品,我想了解为什么它不起作用。

谢谢

标签: powershell

解决方案


转义序列已添加到 PowerShell 6 中的字符串扩展语法中,并且在任何版本的 Windows PowerShell`u{XXXX}中都不会被识别。


在 Windows PowerShell 中,您可以将数值转换[char]为子表达式:

"$([char]0x48)"

或使用-as运算符转换为[char]

0x48 -as [char]
# or
0x48 -as 'char'

如果您尝试在不使用,或在源代码中生成[char]值,则 PowerShell 中有很多机会,的别名和 member-invocation-via-wildcards 特别方便:HhH[]ForEach-Object

# grab the `h` in `$env:path`
(dir env:pat?).Name.GetEnumerator()|select -Last 1

# reflect against another existing [char] to get the type
$g = 'g'|% ToC?ar ($null)
0x48 -as $g.GetType()

# generate char range between G and I, filter out G and I (pwsh >6.1 only)
'G'..'I'|?{$_-notin'G','I'}

推荐阅读