Powershell:替换为西里尔字符串

标签 powershell powershell-2.0 powershell-3.0 powershell-4.0

我无法成功替换西里尔字符串。

我的文件是用带BOM的UTF-8编码的/

powershell脚本代码:

$Title = "название видео"
$Title = $Title.Replace("название", "")
Write-Host $Title
cmd /c pause

结果:

Resulting string

最佳答案

解决方法:

   function ConvertTo-Encoding ([string]$From, [string]$To){
        Begin{
            $encFrom = [System.Text.Encoding]::GetEncoding($from)
            $encTo = [System.Text.Encoding]::GetEncoding($to)
        }
        Process{
            $bytes = $encTo.GetBytes($_)
            $bytes = [System.Text.Encoding]::Convert($encFrom, $encTo, $bytes)
            $encTo.GetString($bytes)
        }
    }

    [System.Text.Encoding]::Default.Codepage

    $Title = "название видео" | ConvertTo-Encoding "UTF-8" "windows-1251"
    Write-Host $Title
    $Title = $Title.Replace(("название" | ConvertTo-Encoding "UTF-8" "windows-1251"), "")
    Write-Host $Title
    cmd /c pause

关于Powershell:替换为西里尔字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39087491/

相关文章:

Powershell 没有返回正确的 csv 值

powershell-2.0 - 在PowerShell 2.0中加载.NET 4.0 beta2程序集

powershell - Import-PSSession : Proxy creation has been skipped for '%' command,,因为PowerShell无法验证其名称的安全性

powershell - 范围标题必须使用适当的属性或方法

powershell - 如何通过 Invoke-Command 在远程计算机上调用 Powershell 版本 2

powershell - 如何验证共享是否具有写权限?

powershell - 如何在PowerShell中读取ATOM XML

powershell - ConvertFrom-String 返回符号而不是文本

shell - 如何从 PowerShell 快捷方式启动带有 "default"颜色的 powershell.exe?

powershell - 安全移除 USB 驱动器