Java String endsWith() 方法

定义和用法

endsWith() 方法检查字符串是否以指定字符结尾。

提示:请使用 startsWith() 方法检查字符串是否以指定字符开头。

实例

判断字符串是否以指定字符结尾:

String myStr = "Hello";
System.out.println(myStr.endsWith("Hel"));   // false
System.out.println(myStr.endsWith("llo"));   // true
System.out.println(myStr.endsWith("o"));     // true

亲自试一试

语法

public boolean endsWith(String chars)

参数

参数 描述
chars String,表示要检查的字符。

技术细节

返回:

boolean 值:

  • true - 如果字符串以指定字符结尾
  • false - 如果字符串不以指定字符结尾