将 Unicode 字符转换为 ASCII (.NET) 中最接近(最相似)的字符

问题描述 投票:0回答:3

如何将不同的 Unicode 字符转换为其最接近的 ASCII 等效字符?就像 ä -> A。我用谷歌搜索但没有找到任何合适的解决方案。这个技巧

Encoding.ASCII.GetBytes("Ä")[0]
没有奏效。 (结果是
?
)。

我发现有一个类

Encoder
具有
Fallback
属性,恰好适用于
char
无法转换的情况,但实现(
EncoderReplacementFallback
)很愚蠢并转换为
?

有什么想法吗?

.net unicode encoding ascii
3个回答
9
投票

如果只是删除变音符,那么请前往这个答案

static string RemoveDiacritics(string stIn) {
  string stFormD = stIn.Normalize(NormalizationForm.FormD);
  StringBuilder sb = new StringBuilder();

  for(int ich = 0; ich < stFormD.Length; ich++) {
    UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
    if(uc != UnicodeCategory.NonSpacingMark) {
      sb.Append(stFormD[ich]);
    }
  }

  return(sb.ToString().Normalize(NormalizationForm.FormC));
}

2
投票

MS Dynamics 存在一个问题,它不允许 x20 到 x7f 之外的任何字符,并且该范围内的某些字符也是无效的。我的答案是创建一个以无效字符为键的数组,返回有效字符的最佳猜测。
它不太漂亮,但它有效。

Function PlainAscii(InText)
Dim i, c, a
Const cUTF7 = "^[\x20-\x7e]+$"
Const IgnoreCase = False
    PlainAscii = ""
    If InText = "" Then Exit Function
    If RegExTest(InText, cUTF7, IgnoreCase) Then
        PlainAscii = InText
    Else
        For i = 1 To Len(InText)
            c = Mid(InText, i, 1)
            a = Asc(c)
            If a = 10 Or a = 13 Or a = 9 Then
                ' Do Nothing - Allow LF, CR & TAB
            ElseIf a < 32 Then
                c = " "
            ElseIf a > 126 Then
                c = CvtToAscii(a)
            End If
            PlainAscii = PlainAscii & c
        Next
    End If
End Function

Function CvtToAscii(inChar)
' Maps The Characters With The 8th Bit Set To 7 Bit Characters
Dim arrChars
    arrChars = Array(" ", " ", "$", " ", ",", "f", """", " ", "t", "t", "^", "%", "S", "<", "O", " ", "Z", " ", " ", "'", "'", """", """", ".", "-", "-", "~", "T", "S", ">", "o", " ", "Z", "Y", " ", "!", "$", "$", "o", "$", "|", "S", " ", "c", " ", " ", " ", "_", "R", "_", ".", " ", " ", " ", " ", "u", "P", ".", ",", "i", " ", " ", " ", " ", " ", " ", "A", "A", "A", "A", "A", "A", "A", "C", "E", "E", "E", "E", "I", "I", "I", "I", "D", "N", "O", "O", "O", "O", "O", "X", "O", "U", "U", "U", "U", "Y", "b", "B", "a", "a", "a", "a", "a", "a", "a", "c", "e", "e", "e", "e", "i", "i", "i", "i", "o", "n", "o", "o", "o", "o", "o", "/", "O", "u", "u", "u", "u", "y", "p", "y")
    CvtToAscii = arrChars(inChar - 127)
End Function

Function RegExTest(ByVal strStringToSearch, strExpression, IgnoreCase)
Dim objRegEx
    On Error Resume Next
    Err.Clear
    strStringToSearch = Replace(Replace(strStringToSearch, vbCr, ""), vbLf, "")
    RegExTest = False
    Set objRegEx = New RegExp
    With objRegEx
        .Pattern = strExpression    '//the reg expression that should be searched for
        If Err.Number = 0 Then
            .IgnoreCase = CBool(IgnoreCase)    '//not case sensitive
            .Global = True              '//match all instances of pattern
            RegExTest = .Test(strStringToSearch)
        End If
    End With
    Set objRegEx = Nothing
    On Error Goto 0
End Function

你的答案必然会有所不同。


0
投票

我做了一个非常简单的解决方案,可以很容易地推断到任何语言,而不需要外部库。我认为它并不完整,也不是 100% 适合任何情况,但它的工作相当不错。

由于示例比解释更具说明性,我在这里直接展示示例:

我的俄语名字“Alexei”用西里尔字母写成:“Алексей”。 我将每个字符转换为 ASCII 代码,与拉丁字符相比,该代码相当高:

"Алексей" → [1040,1083,1077,1082,1089,1077,1081];

现在我只需要在查找数组中查找这些位置即可:

Lookup[1040] = "A"
Lookup[1083] = "l"
Lookup[1077] = "e"
Lookup[1082] = "k"
Lookup[1089] = "s"
Lookup[1077] = "e"
Lookup[1081] = "y"

所以我的代码能够转换:

"Алексей" → "Aleksey";

虽然不完美,但可以完成任务。

如果对于任何字符,我的数组为空,那是因为我过去没有见过这个字符,或者我不知道如何将其转换为拉丁字符。在这种情况下,我所做的只是为其编写 HTML 实体代码:

例如,字符 8711(Nabla 运算符 ∇)。该字符直接转换如下:

"∇" → 8711 → [empty] → &#8711;

某些字符被一组字符替换,例如 € → EUR、ß → ss、θ → th、Œ → OE...

神奇的阵法是:

Lookup = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','!','"','#','$','%','&','''','(',')','*','+',',','-','.','/','0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','@','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','[','\',']','^','_',' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','{','|','}','~',' ','EUR',' ',',','f','"','.','+','+','^','%','S','<','O',' ','Z',' ',' ','"','"','"','"','.','-','-','~','T','s','>','o',' ','z','Y',' ','!','cent','GBP','#','YEN','|','S',' ','c','a','<','-','-','R','-','0','+','2','3',' ','m','P','.',',','1','0','>','q','h','t','?','A','A','A','A','A','A','A','C','E','E','E','E','I','I','I','I','D','N','O','O','O','O','O','x','O','U','U','U','U','Y','T','ss','a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','d','n','o','o','o','o','o','/','o','u','u','u','u','y','t','y','A','a','A','a','A','a','C','c','C','c','C','c','C','c','D','d','D','d','E','e','E','e','E','e','E','e','E','e','G','g','G','g','G','g','G','g','H','j','H','j','I','i','I','i','I','i','I','i','I','i','Y','y','J','j','K','k','k','L','l','L','l','L','l','L','l','L','l','N','n','N','n','N','n','n','N','n','O','o','O','o','O','o','OE','oe','R','r','R','r','R','r','S','s','S','s','S','s','S','s','T','t','T','t','T','t','U','u','U','u','U','u','U','u','U','u','U','u','W','w','Y','y','Y','Z','z','Z','z','Z','z','r','b','B','B','b','b','b','C','C','c','D','D','D','d','D','E','e','e','F','f','G','g','H','l','I','K','k','l','l','M','N','n','O','O','o','O','o','P','p','R','S','s','S','s','t','T','t','T','U','u','U','U','Y','y','Z','z','Z','Z','z','z','2','5','5','5','p','|','|','|','!','D','D','d','Y','y','y','N','N','n','A','a','I','i','O','o','U','u','U','u','U','u','U','u','U','u','e','A','a','A','a','A','a','G','g','G','g','K','k','O','o','O','o','z','z','j','D','D','d','G','g','Y','y','M','m','A','a','A','a','O','o','A','a','A','a','E','e','E','e','I','i','I','i','O','o','O','o','R','r','R','r','U','u','U','u','S','s','T','t','Z','z','H','h','n','d','g','g','Z','z','A','a','E','e','O','o','O','o','O','o','O','o','Y','y','l','n','t','J','F','F','A','C','c','L','T','s','z','?','?','B','U','L','E','e','J','j','Q','q','R','r','Y','y','a','a','a','b','c','c','d','d','e','e','e','e','e','e','b','j','g','g','G','g','g','h','h','h','i','i','I','l','l','l','z','m','m','m','n','n','n','o','o','o','f','r','r','r','r','r','r','r','R','R','s','s','f','s','s','t','t','n','o','o','v','w','y','Y','z','z','z','z','?','?','?','C','O','B','d','G','H','J','K','L','q','?','?','?','?','?','t','t','t','f','l','l','?','?','?','?','h','h','j','r','r','r','r','w','y','"','"','"','"','"','"','"','"','"','<','>','^','v','^','^','"','"','"','"','.','_',',',',',':','.',',',',',',',',',',',',','^','^','o',' ','~','~',' ','x','T','l','s','x','c','7','1','F','L','L','l','_','_','=','"','.','.',',',',','.',',','"','"','~',':','"','"',',','.','_','_','_','"','"','^','~','-','-','^','"','"','"','o','"','"','"','"','"','"','"','"','"','"','"','.','.','-','-','"','"','.','-','-','-','_',',',',','.','.','o',',',',',',',',','_','_','_','_','_','_','~','_','_','=','~','-','-','/','/',',','_','.','_','"','"','"','"','"','~','"','"','.','"','_',',',',','~','"','"','_','.',' ','"','"','o','.',',',',','_','"','*','.','.','"','_','-','-','_','-','-','_','a','e','i','o','u','c','d','h','m','r','t','v','x','F','r','T','T','"',',','N','n','?','?',',','c','c','c',';','J','?','?','?','?','"','"','A','*','E','H','I','?','O','?','Y','O','i','A','B','G','D','E','Z','H','Th','I','K','L','M','N','X','O','P','R','?','S','T','Y','Ph','Ch','Ps','O','I','Y','a','e','i','i','n','a','b','g','d','e','z','h','th','i','k','l','m','n','x','o','p','r','s','s','t','y','ph','ch','ps','o','i','u','o','u','o','K','b','Z','G','G','G','F','O','a','Q','q','C','s','F','F','N','N',')',')','W','w','P','p','b','s','S','S','G','g','b','B','T','t','x','p','c','j','Z','e','e','B','b','C','M','m','p','C','C','C','E','Yo','T','G','E','j','I','I','J','L','N','T','K','I','Y','T','A','B','V','G','D','E','Zh','Z','I','Y','K','L','M','N','O','P','R','S','T','U','F','Kh','Ts','Ch','Sh','Shch','b','Y','b','E','Yu','Ya','a','b','v','g','d','e','zh','z','i','y','k','l','m','n','o','p','r','s','t','u','f','kh','ts','ch','sh','shch','b','y','b','e','yu','ya','','yo','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','a','b','g','d','h','v','z','ch','t','y','','k','l','','m','','n','s','a','','p','','ts','k','r','sh','t','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','h','','','','','','','','l','','','','','','','','','','','','','','','','m','','','','','','','','','','','','','','','','r','','','','','','','','s','','','','','','','','sh','','','','','','','','q','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','b','','','','','','','','v','','','','','','','','t','','','','','','','','ch','','','','','','','','h','','','','','','','','','','','','','','','','n','','','','','','','','ny','','','','','','','','a','','','','','','','','k','','','','','','','','','','','','','','','','kh','','','','','','','','','','','','','','','','w','','','','','','','','a','','','','','','','','z','','','','','','','','zh','','','','','','','','y','','','','','','','','d','','','','','','','','','','','','','','','','j','','','','','','','','g','','','','','','','','','','','','','','','','','','','','','','','','t','','','','','','','','ch','','','','','','','','p','','','','','','','','ts','','','','','','','','ts','','','','','','','','f','','','','','','','','p'];
© www.soinside.com 2019 - 2024. All rights reserved.