
C# EndsWith()简介
在C#中,用于检查一个给定的字符串是否与字符串的结尾相匹配的方法被称为EndsWith()方法。如果给定的字符串与字符串的结尾相匹配,则返回True;如果给定的字符串与字符串的结尾不匹配,则返回False,这标志着返回类型是System.Boolean,并引发ArgumentNullException。如果必须匹配字符串结尾的输入字符串是空的,也可以使用此方法检查大小写敏感性和文化敏感性的比较。
语法
C# EndsWith()方法的语法如下。
public bool EndsWith(String string) public bool EndsWith(String, Boolean, CultureInfo) public bool EndsWith (String, StringComparison)?
其中string是一个指定的字符串,必须与之匹配的字符串的结尾。
如果我们希望忽略字母的大小写,则将Boolean设置为true;如果我们希望考虑字母的大小写,则将其设置为false。
CultureInfo描述了指定字符串和代表字符串结尾的字符串的比较方式。
StringComparison是一个枚举值,描述了指定字符串和代表字符串结尾的字符串的比较方式。
C# EndsWith()方法的工作原理
- 每当需要比较给定的字符串和代表字符串结尾的字符串,以找出代表字符串结尾的字符串是否与给定的字符串结尾相符时,我们就会使用C#中的EndsWith()方法。
- 如果给定的字符串和代表字符串结尾的字符串与给定字符串的结尾相匹配,使用EndsWith()方法返回的值为真。
- 如果给定的字符串和代表字符串结尾的字符串与给定字符串的结尾不匹配,那么使用EndsWith()方法返回的值为假。
- 如果要与代表字符串结尾的字符串匹配的给定字符串是空的,就会引发ArgumentNullException。
C# EndsWith()的例子
下面提到了不同的例子。
例子#1
演示EndsWith()方法的C#程序,该方法将给定的字符串与代表字符串结尾的字符串相匹配。
代码。
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)); } }
输出。

在上述程序中,定义了一个名为program的类。然后调用main方法,在这个方法中,一个字符串变量被用来存储必须与代表字符串结尾的字符串进行比较的字符串。然后,另一个字符串变量被用来存储要与给定字符串的结尾进行比较的字符串。然后,再次使用另一个字符串变量来存储字符串的结尾,以便与给定字符串的结尾进行比较。然后,EndsWith()方法被用来比较给定字符串的结尾和代表给定字符串结尾的字符串,根据给定字符串的结尾是否与代表字符串结尾的字符串匹配,返回真或假。
例子#2
C#程序演示EndsWith()方法,将给定的字符串与代表字符串结尾的字符串与给定的字符串结尾相匹配
代码。
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)); } }
输出。

在上面的程序中,定义了一个名为program的类。然后调用main方法,在这个方法中,一个字符串变量被用来存储必须与代表字符串结尾的字符串进行比较的字符串。然后,另一个字符串变量被用来存储要与给定字符串的结尾进行比较的字符串。然后,再次使用另一个字符串变量来存储字符串的结尾,以便与给定字符串的结尾进行比较。然后,EndsWith()方法被用来比较给定字符串的结尾和代表给定字符串结尾的字符串,根据给定字符串的结尾是否与代表字符串结尾的字符串匹配,返回真或假。最后,输出结果显示在上面的快照中。