C# String Array Contains Ignore Case [Differnet methods with examples]

In this C# tutorial, we will see C# String Array Contains Ignore Case with examples.

When working with string arrays in C#, you may often find the need to check if the array contains a specific string. However, in scenarios where case sensitivity is not important, you may want to perform a case-insensitive search. C# doesn’t provide a built-in method for this specific task, but there are several ways to achieve it.

C# String Array Contains Ignore Case

Let us explore more with different methods and practical examples.

Using LINQ Any with StringComparison

The most straightforward way to perform a case-insensitive search in a string array is by using LINQ’s Any method combined with StringComparison.OrdinalIgnoreCase.

Here’s an example:

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string[] usaNames = { "John", "Michael", "Sarah", "Emily" };
        string targetName = "sarah";

        bool contains = usaNames.Any(name => name.Equals(targetName, StringComparison.OrdinalIgnoreCase));

        Console.WriteLine($"Is the name in the list? {contains}");
    }
}

In this example, contains would be set to true, even though the targetName is in lowercase and the array contains “Sarah” in title case.

You can check the screenshot below where I have executed the code in a console application.

c# string array contains ignore case

sing Array.Find Method

Another way is to use the Array.Find method, which returns the first occurrence of an item in an array that matches the given predicate.

using System;

class Program
{
    static void Main()
    {
        string[] usaNames = { "John", "Michael", "Sarah", "Emily" };
        string targetName = "MICHAEL";

        string foundName = Array.Find(usaNames, name => name.Equals(targetName, StringComparison.OrdinalIgnoreCase));

        if (foundName != null)
        {
            Console.WriteLine("Name found in the list.");
        }
        else
        {
            Console.WriteLine("Name not found in the list.");
        }
    }
}

Using Array.Exists Method

The Array.Exists method can also be used for this purpose. It’s similar to Array.Find but returns a bool indicating whether the element exists or not.

using System;

class Program
{
    static void Main()
    {
        string[] usaNames = { "John", "Michael", "Sarah", "Emily" };
        string targetName = "john";

        bool exists = Array.Exists(usaNames, name => name.Equals(targetName, StringComparison.OrdinalIgnoreCase));

        Console.WriteLine($"Does the name exist? {exists}");
    }
}

Using Array.IndexOf Method with StringComparison

While Array.IndexOf is case-sensitive by default, you can easily turn it into a case-insensitive check by looping through the array and comparing each item manually.

using System;

class Program
{
    static void Main()
    {
        string[] usaNames = { "John", "Michael", "Sarah", "Emily" };
        string targetName = "EMILY";

        int index = -1;
        for (int i = 0; i < usaNames.Length; i++)
        {
            if (usaNames[i].Equals(targetName, StringComparison.OrdinalIgnoreCase))
            {
                index = i;
                break;
            }
        }

        Console.WriteLine($"Is the name in the list? {(index != -1 ? "Yes" : "No")}");
    }
}

Summary

Checking if a string array contains a specific string while ignoring case sensitivity can be achieved in multiple ways in C#. In this tutorial, we saw 4 different methods with examples for “c# string array contains ignore case“.

  • The LINQ Any method is clean and straightforward.
  • Array.Find allows you to find the actual string that matches.
  • Array.Exists returns a bool, indicating existence.
  • Manual Array.IndexOf gives you more control over the comparison.

You may also like the following tutorials: