code字符以ASCII最接近

code字符以ASCII最接近

本文介绍了转换的Uni code字符以ASCII最接近(最相似)字符(.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何以不同的Uni code字符转换为与其最接近的ASCII等效?像 - >答:我用Google搜索,但没有找到合适的解决方案。诀窍 Encoding.ASCII.GetBytes(A)[0] 没有工作。 (结果为)。

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.ASCII.GetBytes("Ä")[0] didn't work. (Result was ?).

我发现,有一类恩codeR 具有后备属性,是完全的当字符不能转换的情况下,却实现(<$ C C $>恩coderReplacementFallback )是愚蠢的,并转换为

I found that there is a class Encoder that has a Fallback property that is exactly for cases when char can't be converted, but implementations (EncoderReplacementFallback) are stupid and convert to ?.

任何想法?

推荐答案

如果它仅仅是去掉href="http://en.wikipedia.org/wiki/Diacritic">变音符号中的this回答:

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

这篇关于转换的Uni code字符以ASCII最接近(最相似)字符(.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:54