如何在 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!
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP