Converting Unicode string to ASCII

后端 未结 2 1461
忘掉有多难
忘掉有多难 2020-12-19 20:14

I have strings containing characters which are not found in ASCII; such as á, é, í, ó, ú; and I need a function to convert them into something acceptable such as a, e, i, o,

2条回答
  •  臣服心动
    2020-12-19 21:14

    Taking this answer from a C#/.Net question it seems to work in PowerShell ported roughly like this:

    function Remove-Diacritics
    {
        Param([string]$Text)
    
    
        $chars = $Text.Normalize([System.Text.NormalizationForm]::FormD).GetEnumerator().Where{ 
    
            [System.Char]::GetUnicodeCategory($_) -ne [System.Globalization.UnicodeCategory]::NonSpacingMark
    
        }
    
    
        (-join $chars).Normalize([System.Text.NormalizationForm]::FormC)
    
    }
    

    e.g.

    PS C:\> Remove-Diacritics 'abcdeéfg'
    abcdeefg
    

提交回复
热议问题