代码之家  ›  专栏  ›  技术社区  ›  GateKiller

是否有C#不区分大小写的equals运算符?

  •  144
  • GateKiller  · 技术社区  · 15 年前

    if (StringA == StringB) {
    

    那么,是否有一个运算符会以不敏感的方式比较两个字符串?

    13 回复  |  直到 14 年前
        1
  •  304
  •   John Feminella    15 年前

    string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
    
        2
  •  44
  •   Pavel Vladov    6 年前

    最好的办法 要比较两个字符串,忽略字母的大小写,请使用 String.Equals 指定序号忽略大小写字符串比较的静态方法。这也是最快的方法,比将字符串转换为小写或大写并在之后进行比较要快得多。

    我测试了这两种方法的性能,并进行了顺序忽略大小写字符串比较 ! 它也比将字符串转换为小写或大写更可靠(请查看Turkish i问题)。因此,始终使用 用于比较字符串是否相等的方法:

    String.Equals(string1, string2, StringComparison.OrdinalIgnoreCase);
    

    如果要执行特定于区域性的字符串比较,可以使用以下代码:

    String.Equals(string1, string2, StringComparison.CurrentCultureIgnoreCase);
    

    欲了解更多信息, read the full story on my blog .

        3
  •  20
  •   Ryan Lundy    15 年前

    上有许多属性 StringComparer 静态类,该类为您可能需要的任何类型的区分大小写返回比较器:

    StringComparer Properties

    例如,你可以打电话

    StringComparer.CurrentCultureIgnoreCase.Equals(string1, string2)
    

    StringComparer.CurrentCultureIgnoreCase.Compare(string1, string2)
    

    它比房子干净一点 string.Equals string.Compare StringComparison 论点

        4
  •  14
  •   leppie    15 年前
    System.Collections.CaseInsensitiveComparer
    

    System.StringComparer.OrdinalIgnoreCase
    
        5
  •  9
  •   Erick    15 年前
    string.Equals(StringA, StringB, StringComparison.CurrentCultureIgnoreCase);
    
        6
  •  8
  •   Grzenio    15 年前

    if (StringA.Equals(StringB, StringComparison.CurrentCultureIgnoreCase)) {
    

    但是您需要确保StringA不是null。所以最好使用:

    string.Equals(StringA , StringB, StringComparison.CurrentCultureIgnoreCase);
    

    编辑:修正了错误

        7
  •  4
  •   Andy Mikula Eric Mickelsen    15 年前

    你可以用

    if (stringA.equals(StringB, StringComparison.CurrentCultureIgnoreCase))
    
        8
  •  3
  •   John Leidegren    15 年前

    操作人员不,但我认为您可以更改区域性,以便字符串比较不区分大小写。

    // you'll want to change this...
    System.Threading.Thread.CurrentThread.CurrentCulture
    // and you'll want to custimize this
    System.Globalization.CultureInfo.CompareInfo
    

    我相信它将改变通过equals运算符比较字符串的方式。

        9
  •  3
  •   renouve    6 年前

    public class IgnoreCase
    {
        private readonly string _value;
    
        public IgnoreCase(string s)
        {
            _value = s;
        }
    
        protected bool Equals(IgnoreCase other)
        {
            return this == other;
        }
    
        public override bool Equals(object obj)
        {
            return obj != null &&
                   (ReferenceEquals(this, obj) || (obj.GetType() == GetType() && this == (IgnoreCase) obj));
        }
    
        public override int GetHashCode()
        {
            return _value?.GetHashCode() ?? 0;
        }
    
        public static bool operator ==(IgnoreCase a, IgnoreCase b)
        {
            return string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
        }
    
        public static bool operator !=(IgnoreCase a, IgnoreCase b)
        {
            return !(a == b);
        }
    
        public static implicit operator string(IgnoreCase s)
        {
            return s._value;
        }
    
        public static implicit operator IgnoreCase(string s)
        {
            return new IgnoreCase(s);
        }
    }
    

    例如:

    Console.WriteLine((IgnoreCase) "a" == "b"); // false
    Console.WriteLine((IgnoreCase) "abc" == "abC"); // true
    Console.WriteLine((IgnoreCase) "Abc" == "aBc"); // true
    Console.WriteLine((IgnoreCase) "ABC" == "ABC"); // true
    
        10
  •  1
  •   Valamas ConsultUtah    11 年前

    我习惯于在这些比较方法的末尾键入: , StringComparison.

    namespace System
    {   public static class StringExtension
        {
            public static bool Equals(this string thisString, string compareString,
                 StringComparison stringComparison)
            {
                return string.Equals(thisString, compareString, stringComparison);
            }
        }
    }
    

    只需注意,您需要在上检查null thisString 在呼叫分机之前。

        11
  •  0
  •   user25623    15 年前
    string.Compare(string1, string2, true)
    
        12
  •  0
  •   Paul Wicks asgeo1    15 年前
    if (StringA.ToUpperInvariant() == StringB.ToUpperInvariant()) {
    

        13
  •  0
  •   TarmoPikaro    6 年前

    其他答案在这里是完全正确的,但不知何故,输入需要一些时间 StringComparison.OrdinalIgnoreCase 而且还使用 String.Compare .

    我编写了一个简单的字符串扩展方法,在这个方法中,您可以使用布尔值指定比较是区分大小写还是不区分大小写-请参见以下答案:

    https://stackoverflow.com/a/49208128/2338477

        14
  •  -1
  •   DjAntony Comanchero    3 年前

    //您可以通过以下方式使其不区分大小写: s1.ToLower()==s2.ToLower();