How to Compare String Value with String Array in C#?

In this C# tutorial, we will discuss, how to compare string value with string array in C#. If you need to compare a single string value with an array of strings to find out if it matches any of the elements, keep reading.

Compare String Value with String Array in C#

Let us check out a few methods to compare string values with string arrays in C#.

1. Using a Loop

One of the most straightforward ways to compare a string with each element in a string array is to loop through the array and use the string.Equals method in C#.

using System;

namespace StringArrayComparison
{
    class Program
    {
        static void Main(string[] args)
        {
            string targetString = "John";
            string[] usaNames = { "Alice", "Bob", "Catherine", "David", "John" };

            foreach (string name in usaNames)
            {
                if (string.Equals(targetString, name))
                {
                    Console.WriteLine($"Found {targetString} in the array.");
                    return;
                }
            }

            Console.WriteLine($"{targetString} was not found in the array.");
        }
    }
}

In the above example, the program loops through each name in the usaNames array and compares it with targetString using string.Equals. When a match is found, it prints a message and exits the loop.

You may like the following screenshot below:

compare string value with string array in c#

2. Using LINQ

The Language Integrated Query (LINQ) provides a more elegant and concise way to compare string value with string array in C#.

using System;
using System.Linq;

namespace StringArrayComparison
{
    class Program
    {
        static void Main(string[] args)
        {
            string targetString = "John";
            string[] usaNames = { "Alice", "Bob", "Catherine", "David", "John" };

            if (usaNames.Contains(targetString))
            {
                Console.WriteLine($"Found {targetString} in the array.");
            }
            else
            {
                Console.WriteLine($"{targetString} was not found in the array.");
            }
        }
    }
}

In this example, we use the Contains method from the LINQ library, which returns a boolean indicating if the targetString exists in usaNames.

3. Using Array.Exists

The Array.Exists method is another straightforward way to compare string value with string array in C#. It takes a predicate as an argument to specify the comparison logic.

using System;

namespace StringArrayComparison
{
    class Program
    {
        static void Main(string[] args)
        {
            string targetString = "John";
            string[] usaNames = { "Alice", "Bob", "Catherine", "David", "John" };

            bool exists = Array.Exists(usaNames, name => name == targetString);

            if (exists)
            {
                Console.WriteLine($"Found {targetString} in the array.");
            }
            else
            {
                Console.WriteLine($"{targetString} was not found in the array.");
            }
        }
    }
}

In this example, we use Array.Exists along with a lambda expression to compare targetString with each element in usaNames.

Here is a screenshot of the output I got after running the code using Visual Studio.

how to compare string value with string array in c#

Case Sensitivity

It’s worth mentioning that the string comparison methods used in the examples above are case-sensitive by default. To make them case-insensitive, you can pass StringComparison.OrdinalIgnoreCase as an additional argument to string.Equals or convert both the string and the array elements to lower or upper case before comparison.

if (string.Equals(targetString, name, StringComparison.OrdinalIgnoreCase))
{
    Console.WriteLine($"Found {targetString} in the array.");
    return;
}

Conclusion

C# offers multiple ways to compare a string with an array of strings. Here we discuss the below 4 methods:

  1. Using a Loop
  2. Using LINQ
  3. Using Array.Exists

You may also like the following tutorials: