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.
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 abool
, indicating existence.- Manual
Array.IndexOf
gives you more control over the comparison.
You may also like the following tutorials:
- Dynamic Arrays in C# with Examples
- C# String Array Contains Wildcard
- How to Check If C# String Array Contains Null?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…