C# String Array Contains Substring [Various Methods With Examples]

In this C# tutorial, I will explain, how to check if a C# string array contains substring with various methods. So let us check the “c# string array contains substring” with various examples.

C# string array contains substring

Now, let us check out, how to check if a C# string array contains a substring using various methods.

Method 1: Using Foreach Loop

The most straightforward method is to manually iterate over each string in the array and use the Contains method to check for the substring in C#. We can simply use the foreach loop.

using System;

class Program
{
    static void Main()
    {
        string[] usaNames = { "Washington", "California", "Texas", "New York", "Florida" };
        string substringToCheck = "New";
        
        foreach (var name in usaNames)
        {
            if (name.Contains(substringToCheck))
            {
                Console.WriteLine($"Found the substring '{substringToCheck}' in '{name}'.");
            }
        }
    }
}

Once you run the code using visual studio console application, you can see the output below:

c# string array contains substring

Method 2: Using LINQ

The Language Integrated Query (LINQ) library provides a more concise way to achieve this task. You can use the Any method to check for the existence of a substring in any of the array elements in C#.

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string[] usaNames = { "Washington", "California", "Texas", "New York", "Florida" };
        string substringToCheck = "New";
        
        bool result = usaNames.Any(name => name.Contains(substringToCheck));
        
        if (result)
        {
            Console.WriteLine($"The substring '{substringToCheck}' exists in the array.");
        }
        else
        {
            Console.WriteLine($"The substring '{substringToCheck}' does not exist in the array.");
        }
    }
}

Method 3: Using Array.Find

You can also use the Array.Find method from the System namespace in C#. This method returns the first element that matches the given condition.

using System;

class Program
{
    static void Main()
    {
        string[] usaNames = { "Washington", "California", "Texas", "New York", "Florida" };
        string substringToCheck = "New";
        
        string foundName = Array.Find(usaNames, name => name.Contains(substringToCheck));
        
        if (!string.IsNullOrEmpty(foundName))
        {
            Console.WriteLine($"Found the substring '{substringToCheck}' in '{foundName}'.");
        }
        else
        {
            Console.WriteLine($"The substring '{substringToCheck}' does not exist in the array.");
        }
    }
}

After I executed the code in Visual Studio, you can see the output below:

Check if c# string array contains substring

Conclusion

Checking for a substring within an array of strings in C# is a common but essential task. You can achieve this through various methods, such as using a simple loop, leveraging LINQ, or utilizing Array.Find. I hope you got an idea about the topic, c# string array contains substring.

You may also like the following tutorials: