首頁  >  文章  >  Java  >  在Java中如何檢查字串是否以特定子字串結尾?

在Java中如何檢查字串是否以特定子字串結尾?

PHPz
PHPz轉載
2023-09-19 20:05:021282瀏覽

在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.com。如有侵權,請聯絡admin@php.cn刪除