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

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

    • <bdo id='GoQbW'></bdo><ul id='GoQbW'></ul>

    1. <legend id='GoQbW'><style id='GoQbW'><dir id='GoQbW'><q id='GoQbW'></q></dir></style></legend>

      1. <tfoot id='GoQbW'></tfoot>
      2. 比较 char 忽略大小写的正确方法是什么?

        What is the correct way to compare char ignoring case?(比较 char 忽略大小写的正确方法是什么?)
        <i id='RgOFS'><tr id='RgOFS'><dt id='RgOFS'><q id='RgOFS'><span id='RgOFS'><b id='RgOFS'><form id='RgOFS'><ins id='RgOFS'></ins><ul id='RgOFS'></ul><sub id='RgOFS'></sub></form><legend id='RgOFS'></legend><bdo id='RgOFS'><pre id='RgOFS'><center id='RgOFS'></center></pre></bdo></b><th id='RgOFS'></th></span></q></dt></tr></i><div id='RgOFS'><tfoot id='RgOFS'></tfoot><dl id='RgOFS'><fieldset id='RgOFS'></fieldset></dl></div>

            1. <legend id='RgOFS'><style id='RgOFS'><dir id='RgOFS'><q id='RgOFS'></q></dir></style></legend>
                <bdo id='RgOFS'></bdo><ul id='RgOFS'></ul>
                <tfoot id='RgOFS'></tfoot>

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

                    <tbody id='RgOFS'></tbody>
                  本文介绍了比较 char 忽略大小写的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想知道比较两个忽略大小写的字符的正确方法适用于所有文化.另外, Comparer.Default 是在不忽略大小写的情况下测试两个字符的最佳方法吗?这适用于代理对吗?

                  I'm wondering what the correct way to compare two characters ignoring case that will work for all cultures. Also, is Comparer<char>.Default the best way to test two characters without ignoring case? Does this work for surrogate-pairs?

                  编辑:添加示例 IComparer 实现

                  如果这对任何人有帮助,这就是我决定使用的方法

                  If this helps anyone this is what I've decided to use

                  public class CaseInsensitiveCharComparer : IComparer<char> {
                      private readonly System.Globalization.CultureInfo ci;
                      public CaseInsensitiveCharComparer(System.Globalization.CultureInfo ci) {
                          this.ci = ci;
                      }
                      public CaseInsensitiveCharComparer()
                          : this(System.Globalization.CultureInfo.CurrentCulture) { }
                      public int Compare(char x, char y) {
                          return Char.ToUpper(x, ci) - Char.ToUpper(y, ci);
                      }
                  }
                  
                  // Prints 3
                  Console.WriteLine("This is a test".CountChars('t', new CaseInsensitiveCharComparer()));
                  

                  推荐答案

                  这取决于您所说的为所有文化工作"是什么意思.即使在土耳其,您是否希望i"和I"相等?

                  It depends on what you mean by "work for all cultures". Would you want "i" and "I" to be equal even in Turkey?

                  你可以使用:

                  bool equal = char.ToUpperInvariant(x) == char.ToUpperInvariant(y);
                  

                  ...但是根据您对有效"的理解,我不确定根据所有文化是否有效".

                  ... but I'm not sure whether that "works" according to all cultures by your understanding of "works".

                  当然,您可以将两个字符都转换为字符串,然后对字符串执行任何您想要的比较.效率稍低,但它确实为您提供了框架中可用的所有比较范围:

                  Of course you could convert both characters to strings and then perform whatever comparison you want on the strings. Somewhat less efficient, but it does give you all the range of comparisons available in the framework:

                  bool equal = x.ToString().Equals(y.ToString(), 
                                                   StringComparison.InvariantCultureIgnoreCase);
                  

                  对于代理对,Comparer 无论如何都不可行,因为您没有单个 char.你可以创建一个 Comparer<int> 虽然.

                  For surrogate pairs, a Comparer<char> isn't going to be feasible anyway, because you don't have a single char. You could create a Comparer<int> though.

                  这篇关于比较 char 忽略大小写的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)
                    <tbody id='z5gQl'></tbody>
                      <bdo id='z5gQl'></bdo><ul id='z5gQl'></ul>

                    • <small id='z5gQl'></small><noframes id='z5gQl'>

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

                          1. <legend id='z5gQl'><style id='z5gQl'><dir id='z5gQl'><q id='z5gQl'></q></dir></style></legend>