C# – Check if a String starts with specific Substring

To check if a String starts with specific substring, call StartsWith() on the string instance and pass the substring as argument to this method. The method returns True if the string actually starts with the specified substring, else it returns False.

Reference to C# String.StartsWith() method.

Example

In the following program, we will take a string "abcdef" in mystring variable and check if this string starts with the substring "abc" using String.StartsWith(String) method.

C# Program

using System;
  
class Example {
    static void Main(string[] args) {
        String mystring = "abcdef";
        String substring = "abc";
        Boolean result = mystring.StartsWith(substring);
        Console.WriteLine($"Does the string starts with specified substring? {result}");
    }
}

Output

Does the string starts with specified substring? True
ADVERTISEMENT

Conclusion

In this C# Tutorial, we have learned how to check if given string starts with a specified substring, using String.StartsWith() method.