C#: Using C# To Check If String Contains A String In String Array

You can use the string.Contains() method to check if a string contains another string, and the Array.Exists() method to check if a string array contains a specific string. Here’s an example of how you can do this in C#:

using System;

class Program
{
    static void Main(string[] args)
    {
        string[] stringArray = { "apple", "banana", "orange" };
        string searchString = "apple";

        bool containsString = Array.Exists(stringArray, str => str.Contains(searchString));

        if (containsString)
        {
            Console.WriteLine("String array contains the search string.");
        }
        else
        {
            Console.WriteLine("String array does not contain the search string.");
        }
    }
}

This code checks if the stringArray contains the searchString using the lambda expression str => str.Contains(searchString) as the condition inside the Array.Exists() method. If the string array contains the search string, it sets containsString to true. Otherwise, it sets it to false.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!