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

javaobject oriented programmingprogramming更新于 2025/6/30 22:04:17

String  是一个表示 不可变字符序列 的对象,一旦创建就无法更改。 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!

相关文章


有用资源