dan*_*gph 44 unicode powershell string-literals unicode-literals
如何在PowerShell字符串中编码Unicode字符U + 0048(H)?
在C#中我会这样做:"\u0048"但是这似乎在PowerShell中不起作用.
Sha*_*evy 57
将'\ u'替换为'0x'并将其强制转换为System.Char:
PS > [char]0x0048
H
Run Code Online (Sandbox Code Playgroud)
您还可以使用"$()"语法将Unicode字符嵌入到字符串中:
PS > "Acme$([char]0x2122) Company"
AcmeT Company
Run Code Online (Sandbox Code Playgroud)
其中T是PowerShell对非注册商标字符的表示.
mcl*_*ton 14
根据文档,PowerShell Core 6.0增加了对此转义序列的支持:
PS> "`u{0048}"
H
Run Code Online (Sandbox Code Playgroud)
也许这不是PowerShell方式,但这就是我的工作.我觉得它更清洁.
[regex]::Unescape("\u0048") # Prints H
[regex]::Unescape("\u0048ello") # Prints Hello
Run Code Online (Sandbox Code Playgroud)
对于我们这些仍在 5.1 上并想要使用高阶 Unicode 字符集(这些答案都不起作用)的人,我创建了这个函数,这样您就可以简单地构建字符串,如下所示:
'this is my favourite park ',0x1F3DE,'. It is pretty sweet ',0x1F60A | Unicode
Run Code Online (Sandbox Code Playgroud)
#takes in a stream of strings and integers,
#where integers are unicode codepoints,
#and concatenates these into valid UTF16
Function Unicode {
Begin {
$output=[System.Text.StringBuilder]::new()
}
Process {
$output.Append($(
if ($_ -is [int]) { [char]::ConvertFromUtf32($_) }
else { [string]$_ }
)) | Out-Null
}
End { $output.ToString() }
}
Run Code Online (Sandbox Code Playgroud)
请注意,让这些显示在控制台中是一个完全不同的问题,但如果您输出到Outlook 电子邮件或 Gridview(如下),它就会正常工作(因为 utf16 是 .NET 接口的本机)。
这也意味着如果您更熟悉十进制,您还可以很容易地输出普通控制(不一定是 unicode)字符,因为您实际上不需要使用0x(十六进制)语法来生成整数。'hello',160,'there' | Unicode会在两个单词之间放置一个不间断的空格,就像您所做的0xA0那样。
| 归档时间: |
|
| 查看次数: |
34433 次 |
| 最近记录: |