如何在 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! 

更新於: 2023-12-01

1K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.