How can I make Array.Contains case-insensitive on a string array?

后端 未结 4 604
忘了有多久
忘了有多久 2020-12-07 15:54

I am using the Array.Contains method on a string array. How can I make that case-insensitive?

4条回答
  •  有刺的猬
    2020-12-07 16:59

    Some important notes from my side, or at least putting some distributed info at one place- concerning the tip above with a StringComparer like in:

    if (array.Contains("str", StringComparer.OrdinalIgnoreCase))
    {}
    
    1. array.Contains() is a LINQ extension method and therefore works by standard only with .NET 3.5 or higher, needing:
      using System;
      using System.Linq;

    2. But: in .NET 2.0 the simple Contains() method (without taking case insensitivity into account) is at least possible like this, with a cast:

      if ( ((IList)mydotNet2Array).Contains(“str”) ) {}

      As the Contains() method is part of the IList interface, this works not only with arrays, but also with lists, etc.

提交回复
热议问题