如何在 Java 中检查字符串是否以特定子字符串结尾?


字符串是一个对象,表示一个不可变的字符序列,一旦创建就不能更改。java.lang.String 类可用于创建字符串对象。

我们可以使用 String 类的 endsWith() 方法来检查字符串是否以特定字符串结尾,它返回一个布尔值 true 或 false。

语法

public boolean endsWith(String prefix)

示例

Open Compiler
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!"); } } }

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

输出

Ends with the specified word! 

更新于: 2023-12-01

1K+ 浏览量

开启你的职业生涯

通过完成课程获得认证

立即开始
广告