Java String.endsWith()
方法示例
原文: https://howtodoinjava.com/java/string/java-string-endswith-method/
Java 字符串endsWith()
方法用于检查字符串的后缀。 它验证给定的字符串是否以参数字符串结尾。
1. Java String.endsWith(String str)
方法
检查字符串是否以后缀参数字符串'str'
结尾的 Java 程序。
public class Main
{
public static void main(String[] args)
{
String blogName = "howtodoinjava.com";
System.out.println( blogName.endsWith("com") ); //true
System.out.println( blogName.endsWith("net") ); //false
}
}
程序输出。
true
false
String.endsWith()
方法不接受正则表达式作为参数。 如果我们以正则表达式模式作为参数传递,它将仅被视为普通字符串。
1.1 不允许使用null
方法参数
请注意,不允许将null
作为endsWith()
方法的方法参数。 如果传递了null
,它将抛出NullPointerException
。
public class StringExample
{
public static void main(String[] args)
{
String blogName = "howtodoinjava.com";
System.out.println( blogName.endsWith(null) );
}
}
程序输出:
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.endsWith(String.java:1436)
at com.StringExample.main(Main.java:11)
参考: