C# string array contains startswith [With Example]

In this C# tutorial, I have taken a complete example of “c# string array contains startswith“. In C#, arrays of strings are used to store multiple string values, and methods like Contains() and StartsWith() are often used to manipulate or query them.

C# string array contains startswith

Let us check out in detail with examples.

Before we get into the Contains() and StartsWith() methods in C#, let’s first look at how to define and initialize a string array in C#.

string[] names = { "Alice", "Bob", "Charlie", "David" };

Here, we have created a string array named names and initialized it with four string values.

The Contains() Method

The Contains() method in C# is often used to check if a specific string exists in an array. While C# arrays themselves do not have a built-in Contains() method, you can use the System.Linq namespace to use Contains() on arrays.

Here’s an example:

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string[] names = { "Alice", "Bob", "Charlie", "David" };
        
        if (names.Contains("Bob"))
        {
            Console.WriteLine("The array contains the name 'Bob'.");
        }
        else
        {
            Console.WriteLine("The array does not contain the name 'Bob'.");
        }
    }
}

The StartsWith() Method

The StartsWith() method in C# checks if a given string starts with a specified substring. Unlike Contains(), the StartsWith() method is directly available on string objects.

Here’s an example that loops through the array to find all names that start with the letter ‘A’:

using System;

class Program
{
    static void Main()
    {
        string[] names = { "Alice", "Bob", "Charlie", "David", "Anna" };

        foreach (string name in names)
        {
            if (name.StartsWith("A"))
            {
                Console.WriteLine($"The name '{name}' starts with 'A'.");
            }
        }
    }
}

Combining Contains() and StartsWith()

Now, let’s combine Contains() and StartsWith() in C# to filter the names that contain the letter ‘i’ and start with the letter ‘D’.

Here’s how you can do it:

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string[] names = { "Alice", "Bob", "Charlie", "David", "Diana" };

        foreach (string name in names)
        {
            if (name.Contains("i") && name.StartsWith("D"))
            {
                Console.WriteLine($"The name '{name}' contains 'i' and starts with 'D'.");
            }
        }
    }
}

Here is a complete example.

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string[] usaNames = { "John", "Emily", "Michael", "Sarah", "William", "Nancy" };

        // Names that contain 'a'
        Console.WriteLine("Names containing the letter 'a':");
        foreach (string name in usaNames)
        {
            if (name.Contains("a"))
            {
                Console.WriteLine(name);
            }
        }

        // Names that start with 'W'
        Console.WriteLine("\nNames that start with 'W':");
        foreach (string name in usaNames)
        {
            if (name.StartsWith("W"))
            {
                Console.WriteLine(name);
            }
        }
    }
}

Once you run the code, you can see the output like the below screenshot.

c# string array contains startswith

Conclusion

The Contains() and StartsWith() methods in C# are extremely useful for manipulating and querying arrays of strings in C#. While Contains() is available via the System.Linq namespace, StartsWith() is a built-in method available on string objects. I hope you get an idea of “c# string array contains startswith“.

You may also like: