Home  >  Article  >  Backend Development  >  C# StartsWith()

C# StartsWith()

PHPz
PHPzOriginal
2024-09-03 15:15:58506browse

The method used to check if there is a match for the beginning instance of the given string with reference to the other string is called StartsWith() method in C#. It returns true if the string matches the beginning instance of the given string and false is returned by using this StartsWith() method. If there is no match for the beginning instance of the given string with reference to the other string and it is possible to check many strings at once by making use of for each loop in C# and the method can also be overloaded with a different number of arguments of different data types passed as parameters to the method.

Syntax:

The syntax of C# StartsWith() method is as follows:

public bool StartsWith(String string_name);

where string_name is the name of the string which is matched for the beginning instance of the given string.

Working of C# StartsWith()

Working of C# StartsWith() method is as follows:

  • Whenever there is a need to determine if the beginning of any string matches the beginning of the given string, we make use of the StartsWith() method in C#.
  • The StartsWith() method in C# is a string method that returns a Boolean value, which is either true or false.
  • The StartsWith() method returns true if the string matches the beginning instance of the given string.
  • The StartsWith() method returns false if the string matches the beginning instance of the given string.

Examples of C# StartsWith()

Following are the examples as given below:

Example #1

C# program to demonstrate the use of StartsWith() method to check if the beginning of any string matches the beginning of the given string:

Code:

using System;
//a class called check is defined
public class check
{
//main method is called within which a string variable is defined to store the string value which is checked to see if there is a match of beginning instance in this string with reference to the other string compared
public static void Main(string[] args)
{
string string1 = "Welcome to C#";
//StartsWith() method is used to check if there is a match to the beginning instance of the given string with reference to the other string passed as a parameter to it
bool bval1 = string1.StartsWith("Welcome");
bool bval2 = string1.StartsWith("w");
Console.WriteLine("The string Welcome matches the beginning instance of the given string Welcome to C#: {0}", bval1);
Console.WriteLine("The string w matches the beginning instance of the given string Welcome to C#: {0}", bval2);
}
}

Output:

C# StartsWith()

In the above program, a class called check is defined. Then the main method is called within which a string variable is defined to store the string value which is checked to see if there is a match of beginning instance in this string with reference to the other string compared. Then StartsWith() method is used to check if there is a match to the beginning instance of the given string with reference to the other string passed as a parameter to it. The first string Welcome is checked against the given string Welcome to C# to find if there is a beginning instance in Welcome to C# matching the string Welcome and the output returned is True because Welcome is present in Welcome to C# whereas when w is checked against the given string Welcome to C# to find if there is a beginning instance in Welcome to C# matching the string w and the output returned is False because w is not present in Welcome to C#.

Example #2

Example 2: C# program to demonstrate the use of StartsWith() method to check if the beginning of any string matches the beginning of the given string:

Code:

using System;
//a class called check is defined
public class check
{
//main method is called within which a string variable is defined to store the string value which is checked to see if there is a match of beginning instance in this string with reference to the other string compared
public static void Main(string[] args)
{
string string1 = "Learning is fun";
//StartsWith() method is used to check if there is a match to the beginning instance of the given string with reference to the other string passed as a parameter to it
bool bval1 = string1.StartsWith("l");
bool bval2 = string1.StartsWith("Learning");
Console.WriteLine("The string l matches the beginning instance of the given string Welcome to C#: {0}", bval1);
Console.WriteLine("The string Learning matches the beginning instance of the given string Welcome to C#: {0}", bval2);
}
}

Output:

C# StartsWith()

In the above program, a class called check is defined. Then the main method is called within which a string variable is defined to store the string value which is checked to see if there is a match of beginning instance in this string with reference to the other string compared. Then StartsWith() method is used to check if there is a match to the beginning instance of the given string with reference to the other string passed as a parameter to it. The first string l is checked against the given string Learning if fun to find if there is a beginning instance in Learning is fun matching the string l and the output returned is False because l is not present in Learning is fun whereas when Learning is checked against the given string Learning is fun to find if there is a beginning instance in Learning is fun matching the string Learning and the output returned is True because Learning is present in Learning is fun.

There are several advantages of using StartsWith() method in C#. They are:

  • The StartsWith() method is used to check the beginning instance of a given string with the reference to the other string considering the case of the letters as well.
  • The StartsWith() method can be used to check the beginning instance of a given string with reference to many strings at once by making use of for each loop.

The above is the detailed content of C# StartsWith(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# String InterpolationNext article:C# String Interpolation