When you want to check if a string starts with a particular substring or string, then look no further. The C# StartsWith()
method provides the solution.
str.StartsWith(match)
str
: This is the string to check.match
: This is the character or string to check at the beginning of str
.The value returned is a boolean. It is true
if match
matches the beginning of str
. Otherwise, false
is returned.
// create classclass StringChecker{// create main methodstatic void Main(){// create stringsstring s1 = "Welcome";string s2 = "to";string s3 = "Edpresso";// check strings above\bool check1 = s1.StartsWith("W"); // truebool check2 = s2.StartsWith("y"); // falsebool check3 = s3.StartsWith("Edp"); // true// print out reuturned valuesSystem.Console.WriteLine(check1);System.Console.WriteLine(check2);System.Console.WriteLine(check3);}}