在PowerShell中拆分表情符号序列 [英] Spliting an emoji sequence in powershell

查看:0
本文介绍了在PowerShell中拆分表情符号序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,将只填充表情符号。没有空格或任何类型的字符。我需要拆分这些表情符号才能识别它们。这是我尝试过的:

function emoji_to_unicode(){
    foreach ($emoji in $textbox.Text) {
        $unicode = [System.Text.Encoding]::Unicode.GetBytes($emoji)
        Write-Host $unicode
    }
}

循环不是逐个打印字节,而是只运行一次,打印所有连接在一起的表情符号的代码。就像所有的表情符号都是一件物品。我测试了6个表情符号,但没有得到这个:

61 216 7 222

61 216 67 222

61 216 10 222

61 216 28 222

61 216 86 220

60 216 174 223

我收到了:

61 216 7 222 61 216 67 222 61 216 10 222 61 216 28 222 61 216 86 220 60 216 174 223

我错过了什么?

推荐答案

字符串只是一个元素。您希望将其更改为字符数组。

foreach ($i in 'hithere') { $i }
hithere

foreach ($i in [char[]]'hithere') { $i }
h
i
t
h
e
r
e

嗯,这个不太好用。这些代码点非常高,U+1F600(32位)等

foreach ($i in [char[]]'😀😁😂😃😄😅😆') { $i }       
�  # 16 bit surrogate pairs?
�
�
�
�
�
�
�
�
�
�
�
�
�
嗯,好的,每一双都加进去。以下是使用https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Surrogates(或者只使用ConvertToUTF32($emoji,0))的另一种方法)

$emojis = '😀😁😂😃😄😅😆'
for ($i = 0; $i -lt $emojis.length; $i += 2) {
  [System.Char]::IsHighSurrogate($emojis[$i])
  0x10000 + ($emojis[$i] - 0xD800) * 0x400 + $emojis[$i+1] - 0xDC00 | % tostring x
  # [system.char]::ConvertToUtf32($emojis,$i) | % tostring x  # or
  $emojis[$i] + $emojis[$i+1]
}


True
1f600
😀
True
1f601
😁
True
1f602
😂
True
1f603
😃
True
1f604
😄
True
1f605
😅
True
1f606
😆

请注意,Unicode.GetBytes()方法调用中的Unicode引用了utf16le编码。

中文作品。

[char[]]'嗨,您好'
嗨
,
您
好
这里使用的是utf32编码。所有字符均为4字节长。将每4个字节转换为int32并将其打印为十六进制。

$emoji = '😀😁😂😃😄😅😆'
$utf32 = [System.Text.Encoding]::utf32.GetBytes($emoji)

for($i = 0; $i -lt $utf32.count; $i += 4) {
    $int32 = [bitconverter]::ToInt32($utf32[$i..($i+3)],0)
    $int32 | % tostring x
}

1f600
1f601
1f602
1f603
1f604
1f605
1f606

或从int32到字符串的另一种方式。简单地将int32转换为[char]不起作用(必须添加成对的[char])。脚本引用:https://www.powershellgallery.com/packages/Emojis/0.1/Content/Emojis.psm1

for ($i = 0x1f600; $i -le 0x1f606; $i++ ) { [System.Char]::ConvertFromUtf32($i) }

😀
😁
😂
😃
😄
😅
😆

另见How to encode 32-bit Unicode characters in a PowerShell string literal?

编辑:

PowerShell 7有一个很好的枚举unes()方法:

$emojis = '😀😁😂😃😄😅😆'
$emojis.enumeraterunes() | % value | % tostring x

1f600
1f601
1f602
1f603
1f604
1f605
1f606

这篇关于在PowerShell中拆分表情符号序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆