- Java.lang Package classes
- Java.lang - Home
- 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 Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - String endsWith() Method
The Java String endsWith() method is, used to determine whether the string ends with the specified suffix string or not. A suffix of a, string is a substring that occurs at the end of the string.
The endsWith() method returns a Boolean value, which is true if the string ends with the specified substring; false otherwise. It accepts a parameter as a string that holds the value of the specified suffix.
This method does not throw any exception while determining whether the string ends with the specified suffix or not.
Syntax
Following is the syntax of the Java String endsWith() method −
public boolean endsWith(String suffix)
Parameters
suffix − This is the suffix.
Return Value
This method returns true if the string ends with the specified suffix.
Checking if String Ends with Given Suffix Example
If the given string value is the same as the specified suffix, the endsWith() method returns true.
In the following program, we are instantiating the String class with the value "Hello". Then, using the endsWith() method, we are, trying to check whether the string ends with the specified suffix "llo" or not.
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)); } }
Output
On executing the above program, it will produce the following result −
The given string is: Hello The suffix is: llo The string ends with the specified suffix or not? true
Checking if String is not Ending with Given Suffix Example
If the given string value is different from the specified suffix, the endsWith() method returns false.
In the following example, we are creating an object of the string class with the value "Java Programming". Using the endsWith() method, we are checking whether the string ends with the specified suffix "Java" or not.
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)); } }
Output
Following is the output of the above program −
The given string is: Java Programming The suffix is: Java The string ends with the specified suffix or not? false
Checking if String is Ending with Given Suffix in Case Sensitive Way Example
If the given string value is the same as the suffix, but the case is different, the endsWith() method returns false.
In the following program, we are instantiating the string class with the value "TutoriaslPoint". Using the endsWith() method, we are trying to check whether the string ends with the specified suffix "point" or not. Since the method is case-sensitive, it will return 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)); } }
Output
The above program, produces the following output −
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
Checking if String is Ending with Given Suffix in Case Sensitive Way Example
Using conditional statement to check whether the string ends with the specified suffix or not.
In this program, we are creating a string literal with the value "TutorialsPoint". Then, using the endsWith() method, we are checking whether the string ends with the specified suffix "Point" or not.
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"); } } }
Output
After executing the above program, it generates the following output −
The given string is: TutorialsPoint The suffix is: Point The string ends with the specified suffix
To Continue Learning Please Login
Login with Google