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

PHPz
PHPz 转载
2023-09-19 20:05:02 921浏览

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

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

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

public boolean endsWith(String prefix)

Example

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!

以上就是在Java中如何检查字符串是否以特定子字符串结尾?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除