企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Java `String.endsWith()`方法示例 > 原文: [https://howtodoinjava.com/java/string/java-string-endswith-method/](https://howtodoinjava.com/java/string/java-string-endswith-method/) [Java 字符串](https://howtodoinjava.com/java-string/)`endsWith()`方法用于检查字符串的后缀。 它验证给定的字符串是否以参数字符串结尾。 ## 1\. Java `String.endsWith(String str)`方法 检查字符串是否以后缀参数字符串`'str'`结尾的 Java 程序。 ```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 } } ``` 程序输出。 ```java true false ``` > `String.endsWith()`方法不接受**正则表达式**作为参数。 如果我们以*正则表达式*模式作为参数传递,它将仅被视为普通字符串。 #### 1.1 不允许使用`null`方法参数 请注意,不允许将`null`作为`endsWith()`方法的方法参数。 如果传递了`null`,它将抛出`NullPointerException`。 ```java public class StringExample { public static void main(String[] args) { String blogName = "howtodoinjava.com"; System.out.println( blogName.endsWith(null) ); } } ``` 程序输出: ```java Exception in thread "main" java.lang.NullPointerException at java.lang.String.endsWith(String.java:1436) at com.StringExample.main(Main.java:11) ``` 参考: [Java `String`文档](https://docs.oracle.com/javase/10/docs/api/java/lang/String.html)