
- Java.lang 包類
- Java.lang - 首頁
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包附加內容
- Java.lang - 介面
- Java.lang - 錯誤
- Java.lang - 異常
- Java.lang 包有用資源
- Java.lang - 有用資源
- Java.lang - 討論
Java - String endsWith() 方法
Java String endsWith() 方法用於確定字串是否以指定的結尾字串結尾。字串的結尾是指出現在字串末尾的子字串。
endsWith() 方法返回一個布林值,如果字串以指定的子字串結尾,則返回 true;否則返回 false。它接受一個引數作為字串,該引數儲存指定結尾的值。
此方法在確定字串是否以指定的結尾結尾時不會丟擲任何異常。
語法
以下是Java String endsWith() 方法的語法:
public boolean endsWith(String suffix)
引數
suffix − 這是結尾。
返回值
如果字串以指定的結尾結尾,則此方法返回 true。
檢查字串是否以給定結尾結尾的示例
如果給定的字串值與指定的結尾相同,則 endsWith() 方法返回true。
在下面的程式中,我們使用值"Hello"例項化 String 類。然後,使用endsWith() 方法,我們嘗試檢查字串是否以指定的結尾"llo"結尾。
package com.tutorialspoint; public class CheckSuffix { public static void main(String[] args) { //instantiate a string class String str = new String("Hello"); //initialize the suffix String suffix = "llo"; System.out.println("The given string is: " + str); System.out.println("The suffix is: " + suffix); //using the endsWith() method System.out.println("The string ends with the specified suffix or not? " + str.endsWith(suffix)); } }
輸出
執行上述程式後,將產生以下結果:
The given string is: Hello The suffix is: llo The string ends with the specified suffix or not? true
檢查字串是否不以給定結尾結尾的示例
如果給定的字串值與指定的結尾不同,則 endsWith() 方法返回false。
在下面的示例中,我們使用值"Java Programming"建立一個字串類的物件。使用endsWith() 方法,我們檢查字串是否以指定的結尾"Java"結尾。
package com.tutorialspoint; public class CheckSuffix { public static void main(String[] args) { //create an object of the string class String str = new String("Java Programming"); //initialize the suffix String suffix = "Java"; System.out.println("The given string is: " + str); System.out.println("The suffix is: " + suffix); //using the endsWith() method System.out.println("The string ends with the specified suffix or not? " + str.endsWith(suffix)); } }
輸出
以下是上述程式的輸出:
The given string is: Java Programming The suffix is: Java The string ends with the specified suffix or not? false
以區分大小寫的方式檢查字串是否以給定結尾結尾的示例
如果給定的字串值與結尾相同,但大小寫不同,則 endsWith() 方法返回false。
在下面的程式中,我們使用值"TutoriaslPoint"例項化字串類。使用endsWith() 方法,我們嘗試檢查字串是否以指定的結尾"point"結尾。由於該方法區分大小寫,因此它將返回false。
package com.tutorialspoint; public class CheckSuffix { public static void main(String[] args) { //instantiate the string class String str = new String("TutorialsPoint"); //initialize the suffix String suffix = "point"; System.out.println("The given string is: " + str); System.out.println("The suffix is: " + suffix); //using the endsWith() method System.out.println("The string ends with the specified suffix or not? " + str.endsWith(suffix)); } }
輸出
上述程式產生以下輸出:
The string is: Welcome The suffix string is: COME When the suffix is the same as the substring but the case are different, it will return: false
以區分大小寫的方式檢查字串是否以給定結尾結尾的示例
使用條件語句檢查字串是否以指定的結尾結尾。
在此程式中,我們建立一個值為"TutorialsPoint"的字串文字。然後,使用endsWith() 方法,我們檢查字串是否以指定的結尾"Point"結尾。
package com.tutorialpoint; public class CheckSuffix { public static void main(String[] args) { //instantiate the string class String str = new String("TutorialsPoint"); //initialize the suffix String suffix = "Point"; System.out.println("The given string is: " + str); System.out.println("The suffix is: " + suffix); //using the endsWith() method boolean bool = str.endsWith(suffix); if(bool) { System.out.println("The string ends with the specified suffix"); } else { System.out.println("The string is not ends with the specified suffix"); } } }
輸出
執行上述程式後,它會生成以下輸出:
The given string is: TutorialsPoint The suffix is: Point The string ends with the specified suffix