C# – Check if a String Starts with Specific Character
To check if a String starts with specific character, call StartsWith()
on the string instance and pass the character as argument to this method. The method returns True if the string actually starts with the specified character, else it returns False.
Reference to C# String.StartsWith() method.
Example
In the following program, we will take a string abcd
and check if this string starts with character a
using String.StartsWith(Char)
method.
C# Program
</>
Copy
using System;
class Example {
static void Main(string[] args) {
String mystring = "abcd";
Char ch = 'a';
Boolean result = mystring.StartsWith(ch);
Console.WriteLine($"Does the string starts with specified char? {result}");
}
}
Output
Does the string starts with specified char? True
Conclusion
In this C# Tutorial, we have learned how to check if given string starts with a specified character, using String.StartsWith() method.