powershell调用ChnCharInfo.dll的时候,是如何处理多音字的?


PowerShell交流中心分类: Questionspowershell调用ChnCharInfo.dll的时候,是如何处理多音字的?
0
IMDQ asked 7 年 ago

本人powershell小白,今天遇到个问题,就是中文名字转换拼音的时候,调用微软提供的ChnCharInfo.dll会出现多音字处理错误的情况,不知道怎么处理。

如下

add-type -path “C:\TextToPinyin\ChnCharInfo.dll”
$inputtxt=”李强”
$shortR=””
$allR=””
foreach($c in $inputtxt.Trim().ToCharArray()){
try {
$chineseChar=New-Object Microsoft.International.Converters.PinYinConverter.ChineseChar($c)
$shortR+=$chineseChar.Pinyins[0].Substring(0, 1).ToLower()
$allR += $chineseChar.Pinyins[0].Substring(0,$chineseChar.Pinyins[0].Length – 1).ToLower()
}
catch {
$shortR+=$c
$allR +=$c
}
}
$allr

 

$allr会变成li jiang