Introduction to 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.
Working of C# EndsWith() Method
- Whenever there is a need to compare the given string and a string representing the end of the string to find out if the string representing the end of the string matches the end of the given string, we make use of the EndsWith() method in C#.
- The value returned by using the EndsWith() method is true if the given string and the string representing the end of the string matches the end of the given string.
- The value returned by using the EndsWith() method is false if the given string and the string representing the end of the string do not match the end of the given string.
- ArgumentNullException is raised if the given string which is to be matched with the string representing the end of the string to match the end of the given string is Null.
Examples of C# EndsWith()
Different examples are mentioned below:
Example #1
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.
Example #2
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.
Recommended Articles
This is a guide to C# EndsWith(). Here we discuss the concept of EndsWith() method in C# through definition, syntax, and working through programming examples and their outputs. You may also have a look at the following articles to learn more –