Convert Unicode char to closest (most similar) char in ASCII (.NET)

后端 未结 2 563
日久生厌
日久生厌 2020-12-06 05:26

How do I to convert different Unicode characters to their closest ASCII equivalents? Like Ä -> A. I googled but didn\'t find any suitable solution. The trick Encoding.

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 06:07

    If it is just removing of the diacritical marks, then head to this answer:

    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));
    }
    

提交回复
热议问题