C# 中的 EndsWith() 方法
csharpserver side programmingprogramming更新于 2025/11/27 17:56:00
C# 中的 EndsWith() 方法用于检查当前字符串实例的结尾是否与指定字符串匹配。
语法
以下是语法 −
public bool EndsWith(string str)
上面,str 参数是要比较的字符串。
示例
现在让我们看一个实现 EndsWith() 方法的示例 −
using System;
public class Demo{
public static void Main(){
bool val;
string str = "demo$";
val = str.EndsWith("$");
Console.WriteLine("Return Value = "+val.ToString());
}
}
输出
这将产生以下输出 −
Return Value = True
示例
现在让我们看另一个实现 EndsWith() 方法的示例 −
using System;
public class Demo{
public static void Main(){
bool val;
string str = "mytext@";
val = str.EndsWith("#");
Console.WriteLine("Return Value = "+val.ToString());
}
}
输出
这将产生以下输出 −
Return Value = False

