<bdo id='Ukt5f'></bdo><ul id='Ukt5f'></ul>

        <small id='Ukt5f'></small><noframes id='Ukt5f'>

      1. <i id='Ukt5f'><tr id='Ukt5f'><dt id='Ukt5f'><q id='Ukt5f'><span id='Ukt5f'><b id='Ukt5f'><form id='Ukt5f'><ins id='Ukt5f'></ins><ul id='Ukt5f'></ul><sub id='Ukt5f'></sub></form><legend id='Ukt5f'></legend><bdo id='Ukt5f'><pre id='Ukt5f'><center id='Ukt5f'></center></pre></bdo></b><th id='Ukt5f'></th></span></q></dt></tr></i><div id='Ukt5f'><tfoot id='Ukt5f'></tfoot><dl id='Ukt5f'><fieldset id='Ukt5f'></fieldset></dl></div>

        <tfoot id='Ukt5f'></tfoot>
      2. <legend id='Ukt5f'><style id='Ukt5f'><dir id='Ukt5f'><q id='Ukt5f'></q></dir></style></legend>

        忽略字符串比较中的重音字母

        时间:2023-08-27
            <tbody id='baZR2'></tbody>

          <small id='baZR2'></small><noframes id='baZR2'>

          <tfoot id='baZR2'></tfoot>
          <i id='baZR2'><tr id='baZR2'><dt id='baZR2'><q id='baZR2'><span id='baZR2'><b id='baZR2'><form id='baZR2'><ins id='baZR2'></ins><ul id='baZR2'></ul><sub id='baZR2'></sub></form><legend id='baZR2'></legend><bdo id='baZR2'><pre id='baZR2'><center id='baZR2'></center></pre></bdo></b><th id='baZR2'></th></span></q></dt></tr></i><div id='baZR2'><tfoot id='baZR2'></tfoot><dl id='baZR2'><fieldset id='baZR2'></fieldset></dl></div>

                • <bdo id='baZR2'></bdo><ul id='baZR2'></ul>
                  <legend id='baZR2'><style id='baZR2'><dir id='baZR2'><q id='baZR2'></q></dir></style></legend>
                  本文介绍了忽略字符串比较中的重音字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要比较 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);
                  

                  这两个字符串必须相同(就我的应用程序而言),但是这两个语句的计算结果都为 false.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?

                  推荐答案

                  EDIT 2012-01-20: Oh boy!解决方案要简单得多,并且几乎永远存在于框架中.正如 knightpfhor 所指出的:

                  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 的博客上 (RIP...).

                  原理是将'é'变成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.

                  你好"变成他<acute>llo",而后者又变成你好".

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

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

                  上一篇:TimeSeries 趋势数据的重采样、聚合和插值 下一篇:如何使用适当的文化信息将字符串转换为双精度

                  相关文章

                  最新文章

                    <i id='2WkYY'><tr id='2WkYY'><dt id='2WkYY'><q id='2WkYY'><span id='2WkYY'><b id='2WkYY'><form id='2WkYY'><ins id='2WkYY'></ins><ul id='2WkYY'></ul><sub id='2WkYY'></sub></form><legend id='2WkYY'></legend><bdo id='2WkYY'><pre id='2WkYY'><center id='2WkYY'></center></pre></bdo></b><th id='2WkYY'></th></span></q></dt></tr></i><div id='2WkYY'><tfoot id='2WkYY'></tfoot><dl id='2WkYY'><fieldset id='2WkYY'></fieldset></dl></div>

                      • <bdo id='2WkYY'></bdo><ul id='2WkYY'></ul>

                      <legend id='2WkYY'><style id='2WkYY'><dir id='2WkYY'><q id='2WkYY'></q></dir></style></legend>
                    1. <tfoot id='2WkYY'></tfoot>

                      <small id='2WkYY'></small><noframes id='2WkYY'>