首页  >  文章  >  后端开发  >  C# EndsWith()

C# EndsWith()

PHPz
PHPz原创
2024-09-03 15:17:36651浏览

用于检查给定字符串是否与字符串末尾匹配的方法在 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;如果我们希望考虑字母大小写,则将 Boolean 设置为 false。

CultureInfo 描述了指定字符串和表示字符串结尾的字符串的比较方式。

StringComparison 是描述如何比较指定字符串和表示字符串结尾的字符串的枚举值之一。

C# EndsWith() 方法的工作原理

  • 每当需要比较给定字符串和代表字符串结尾的字符串以找出代表字符串结尾的字符串是否与给定字符串的结尾匹配时,我们都会使用 EndsWith() C# 中的方法。
  • 如果给定字符串和表示字符串结尾的字符串与给定字符串的结尾匹配,则使用 E​​ndsWith() 方法返回的值为 true。
  • 如果给定字符串和表示字符串结尾的字符串与给定字符串的结尾不匹配,则使用 E​​ndsWith() 方法返回的值为 false。
  • 如果要与表示字符串末尾的字符串匹配的给定字符串为 Null,则引发 ArgumentNullException。

C# EndsWith() 示例

下面提到了不同的示例:

示例#1

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 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));
}
}

输出:

C# EndsWith()

在上面的程序中,定义了一个名为program的类。然后调用main方法,其中使用一个字符串变量来存储字符串,该字符串的末尾必须与代表字符串末尾的字符串进行比较。然后使用另一个字符串变量来存储字符串的结尾,以便与给定字符串的结尾进行比较。然后,另一个字符串变量用于存储字符串的结尾,以便与给定字符串的结尾进行比较。然后 EndsWith() 方法用于比较给定字符串的结尾和表示给定字符串结尾的字符串,根据给定字符串的结尾是否与表示字符串结尾的字符串匹配,返回 true 或 false不。

示例#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));
}
}

输出:

C# EndsWith()

在上面的程序中,定义了一个名为program的类。然后调用main方法,其中使用一个字符串变量来存储字符串,该字符串的末尾必须与代表字符串末尾的字符串进行比较。然后使用另一个字符串变量来存储字符串的结尾,以便与给定字符串的结尾进行比较。然后,另一个字符串变量用于存储字符串的结尾,以便与给定字符串的结尾进行比较。然后 EndsWith() 方法用于比较给定字符串的结尾和表示给定字符串结尾的字符串,根据给定字符串的结尾是否与表示字符串结尾的字符串匹配,返回 true 或 false不是。最后,输出如上面的快照所示。

以上是C# EndsWith()的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:C# intern()下一篇:C# String Split()