c#.NET Checking for an element in an array case insensitive

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhitam30111985
    New Member
    • Aug 2007
    • 112

    c#.NET Checking for an element in an array case insensitive

    Hi all ,


    I found a way to check for an element in a c# array here :



    But this is case sensitive search. Is there a way to do the same thing by ignoring case ?


    Regards ,

    Rhitam
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I'm lazy... I just do all my comparrisons using the .ToLower() method

    Code:
    String bob = "This is a Mixed Case String";
    if (bob.ToLower() contains "mixed")
    {
         // Jump for joy
    }

    Comment

    • kurtzky
      New Member
      • Nov 2008
      • 26

      #3
      C# is case-sensitive so you have to make sure that the value you are comparing should be the same, that means you need to use either to lower or to upper, depending on the value you want to compare.

      Comment

      • ApocDev
        New Member
        • Apr 2009
        • 8

        #4
        Code:
                public string[] Contains(IEnumerable<string> array, string val)
                {
                    return (new List<string>(array)).FindAll(s => s.ToLower() == val.ToLower()).ToArray();
                }
        Or

        Code:
                public string Contains(IEnumerable<string> array, string val)
                {
                    return (new List<string>(array)).Find(s => s.ToLower() == val.ToLower());
                }
        That's what I use at least. :)

        Comment

        Working...