如何在 Java 中检查字符串是否以特定子字符串结尾?
字符串是一个对象,表示一个不可变的字符序列,一旦创建就不能更改。java.lang.String 类可用于创建字符串对象。
我们可以使用 String 类的 endsWith() 方法来检查字符串是否以特定字符串结尾,它返回一个布尔值 true 或 false。
语法
public boolean endsWith(String prefix)
示例
public class StringEndsWithSubStringTest {
public static void main(String[] args) {
String str = "WelcomeToTutorialsPoint";
if(str.endsWith("Point")) {
System.out.println("Ends with the specified word!");
} else {
System.out.println("Does not end with the specified word!");
}
}
}
输出
Ends with the specified word!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP