本文介绍了忽略字符串比较重音字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较在C#2串并把重音字母相同的非重音字母。例如:

I need to compare 2 strings in C# and treat accented letters the same as non-accented letters. For example:

string s1 = "hello";
string s2 = "héllo";

s1.Equals(s2, StringComparison.InvariantCultureIgnoreCase);
s1.Equals(s2, StringComparison.OrdinalIgnoreCase);

2,这些字符串必须是相同的(至于我的应用程序而言),但两者这些语句的计算结果为假。有没有在C#的方式来做到这一点?

These 2 strings need to be the same (as far as my application is concerned), but both of these statements evaluate to false. Is there a way in C# to do this?

推荐答案

编辑2012-01-20:哦,孩子!该解决方案是非常简单的,并已在该框架几乎永远。 指出:

EDIT 2012-01-20: Oh boy! The solution was so much simpler and has been in the framework nearly forever. As pointed out by knightpfhor :

string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace);


下面是从一个字符串剥离附加符号功能:


Here's a function that strips diacritics from a string:

static string RemoveDiacritics(string text)
{
  string formD = text.Normalize(NormalizationForm.FormD);
  StringBuilder sb = new StringBuilder();

  foreach (char ch in formD)
  {
    UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(ch);
    if (uc != UnicodeCategory.NonSpacingMark)
    {
      sb.Append(ch);
    }
  }

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

更多细节上MichKap的博客 )。

其原理是它变成'E'到连续2个字符E,急。
然后,它通过迭代字符,并跳过了变音符号。

The principle is that is it turns 'é' into 2 successive chars 'e', acute.It then iterates through the chars and skips the diacritics.

你好变成了他<急性>劳工组织,进而成为你好

"héllo" becomes "he<acute>llo", which in turn becomes "hello".

Debug.Assert("hello"==RemoveDiacritics("héllo"));


请注意:这里有一个更紧凑.NET4 +相同功能的友好版本:


Note: Here's a more compact .NET4+ friendly version of the same function:

static string RemoveDiacritics(string text)
{
  return string.Concat(
      text.Normalize(NormalizationForm.FormD)
      .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch)!=
                                    UnicodeCategory.NonSpacingMark)
    ).Normalize(NormalizationForm.FormC);
}

这篇关于忽略字符串比较重音字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:34