How to replace ANSI characters with UTF-8 characte

2019-08-20 04:26发布

问题:

I tried running this:

(Get-Content c:\example.srt).replace('æ', 'ć') | Set-Content c:\example.srt

But it only replaces the character æ with ANSI c.

I'd also like to be able to replace more than 1 character at the time.

回答1:

You can do like this:

Replace them in the character group.Put the symbols in the character group.

Get-Content c:\example.srt |% {$_ -replace "[æ\._\.*]", "ć"}

It will replace æ , _ , * with ć.

like:

Get-Date -Format G | foreach {$_ -replace "[:\./]", "_"}

Hope it helps.



回答2:

You can give this a go. It converts a file from Windows-1252 to UTF-8.

$ansi = [System.Text.Encoding]::GetEncoding(1252)

Get-Content -Encoding $ansi "c:\example.srt" | Out-File -Encoding UTF-8 "c:\example.srt"