Home >Backend Development >C#.Net Tutorial >C# EndsWith()
The method used to check if a given string matches the end of the string or not is called EndsWith() method in C#. True is returned if the given string matches the end of the string, and false is returned if the given string does not match the end of the string, which signifies that the return type is System. Boolean and ArgumentNullException is raised. If the input string with which the end of the string must be matched is null, case sensitivity can also be checked along with culture-sensitive comparison using this method.
Syntax
The syntax of the C# EndsWith() method is as follows:
public bool EndsWith(String string) public bool EndsWith(String, Boolean, CultureInfo) public bool EndsWith (String, StringComparison)?
Where string is a specified string with which the end of the string must be matched.
Boolean is set to true if we wish to ignore the case of letters, and it is set to false if we wish to consider the case of letters.
CultureInfo describes the way the specified string and the string representing the end of the string are compared.
StringComparison is one of the enumeration values describing how the specified string and the string representing the end of the string are compared.
Different examples are mentioned below:
C# program to demonstrate EndsWith() method to match the given string with the string representing the end of the string with the end of the given string
Code:
using System; //a class called program is defined public class program { //main method is called public static void Main(string[] args) { //a string variable is used to store the string whose end of the string mjst be compared with the string representing the end of the string string str1 = "C Sharp"; //another string variable is used to store the end of the string to compare with the end of the given string string str2 = "arp"; //another string variable is used to store the end of the string to compare with the end of the given string string str3 = "C"; //EndsWith() method is used to compare the end of the given string and the string representing the end of a given string Console.WriteLine("If the end of the given string matches with the string representing the end of the string:{0} ", str1.EndsWith(str2)); Console.WriteLine("If the end of the given string matches with the string representing the end of the string:{0} ",str1.EndsWith(str3)); } }
Output:
In the above program, a class called program is defined. Then the main method is called, within which a string variable is used to store the string whose end of the string must be compared with the string representing the end of the string. Then another string variable is used to store the end of the string to compare with the end of the given string. Then again, another string variable is used to store the end of the string to compare with the end of the given string. Then EndsWith() method is used to compare the end of the given string and the string representing the end of a given string which returns either true or false depending on if the end of the given string matches the string representing the end of the string or not.
C# program to demonstrate EndsWith() method to match the given string with the string representing the end of the string with the end of the given string
Code:
using System; //a class called program is defined public class program { //main method is called public static void Main(string[] args) { //a string variable is used to store the string whose end of the string must be compared with the string representing the end of the string string str1 = "Learning"; //another string variable is used to store the end of the string to compare with the end of the given string string str2 = "Learn"; //another string variable is used to store the end of the string to compare with the end of the given string string str3 = "ing"; //EndsWith() method is used to compare the end of the given string and the string representing the end of a given string Console.WriteLine("If the end of the given string matches with the string representing the end of the string:{0} ", str1.EndsWith(str2)); Console.WriteLine("If the end of the given string matches with the string representing the end of the string:{0} ",str1.EndsWith(str3)); } }
Output:
In the above program, a class called program is defined. Then the main method is called, within which a string variable is used to store the string whose end of the string must be compared with the string representing the end of the string. Then another string variable is used to store the end of the string to compare with the end of the given string. Then again, another string variable is used to store the end of the string to compare with the end of the given string. Then EndsWith() method is used to compare the end of the given string and the string representing the end of a given string which returns either true or false depending on if the end of the given string matches the string representing the end of the string or not. Finally, the output is shown in the snapshot above.
The above is the detailed content of C# EndsWith(). For more information, please follow other related articles on the PHP Chinese website!