How to Check If a C# String Array Contains a Partial String?

If you to find out if a particular string array contains an element that partially matches a given string, check out this C# tutorial. Here I have explained how to check if a C# string array contains a partial string.

Check If a C# String Array Contains a Partial String

Let us explore different approaches for checking if a C# string array contains an element with a partial string match.

Method-1: Using foreach loop

One of the easiest ways to check if a string array contains a partial string in C# is using foreach loop.

Here is a complete code. You can have a look at it.

using System;

namespace PartialStringMatchExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] usaNames = { "John", "Emily", "William", "Sophia", "Michael" };
            string partialMatch = "li";

            foreach (string name in usaNames)
            {
                if (name.Contains(partialMatch))
                {
                    Console.WriteLine($"Match found: {name}");
                }
            }
        }
    }
}

Once you run the code using a Visual Studio console application, you can see the output in the screenshot below:

Check If a C# String Array Contains partial string

Method-2: Using LINQ

We can also check if a string array contains a partial string in C# using LINQ (Language-Integrated Query) with its Where and Any methods.

Here is the code:

using System;
using System.Linq;

namespace PartialStringMatchLinqExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] usaNames = { "John", "Emily", "William", "Sophia", "Michael" };
            string partialMatch = "li";

            bool isPresent = usaNames.Any(name => name.Contains(partialMatch));
            if (isPresent)
            {
                Console.WriteLine("A partial match is found.");
            }
        }
    }
}

Here, the Any method returns a boolean indicating if any string in the array contains the partial match.

The output for this program will be:

A partial match is found.

Method-3: Using Array.Find

Another option is to use the Array.Find method which returns the first occurrence of a matching string. However, this method would require you to import the System namespace.

using System;

namespace PartialStringMatchArrayFindExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] usaNames = { "John", "Emily", "William", "Sophia", "Michael" };
            string partialMatch = "li";

            string found = Array.Find(usaNames, name => name.Contains(partialMatch));
            if (found != null)
            {
                Console.WriteLine($"Match found: {found}");
            }
        }
    }
}

Method-4: Case-Insensitive Matching

If you’re looking for a case-insensitive match, you can use StringComparison.OrdinalIgnoreCase with string.IndexOf.

using System;

namespace PartialStringMatchCaseInsensitiveExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] usaNames = { "John", "Emily", "William", "Sophia", "Michael" };
            string partialMatch = "Li";

            foreach (string name in usaNames)
            {
                if (name.IndexOf(partialMatch, StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    Console.WriteLine($"Match found: {name}");
                }
            }
        }
    }
}

Conclusion

As you can see, there are multiple ways to check if a C# string array contains a partial string. Whether you prefer loop iteration, LINQ, or Array.Find. I hope you got an idea of “c# string array contains partial string“.

You may also like: