Mastering Java’s String.endsWith(): How to Check String Suffixes with Examples
Java String.endsWith() – Checking a String’s Suffix
The String.endsWith() method determines whether a given string ends with a specified suffix. It returns a boolean: true if the suffix matches the string’s final characters, otherwise false.
Method Signature
public boolean endsWith(String suffix)
Parameter
suffix – The substring to compare against the end of the calling string.
Return Value
- true – The calling string ends with
suffix. - false – The calling string does not end with
suffix.
Exceptions
Throws NullPointerException if suffix is null.
Practical Example
public class EndsWithDemo {
public static void main(String[] args) {
String sample = "Java String endsWith example";
System.out.println("Ends with 'e': " + sample.endsWith("e")); // true
System.out.println("Ends with 'ple': " + sample.endsWith("ple")); // true
System.out.println("Ends with 'Java':" + sample.endsWith("Java")); // false
}
}
Output:
Ends with 'e': true Ends with 'ple': true Ends with 'Java':false
For official documentation, visit String.endsWith().
Java
- Encapsulation in Java: A Comprehensive Guide with Practical Example
- Master Java String Manipulation: Essential Functions, Methods, and Practical Examples
- Java String length() Method: How to Get a String’s Size (Example)
- Mastering Java String.indexOf(): Locating Substrings & Practical Examples
- Understanding Java String.charAt(): Syntax, Return Type, Exceptions, and a Practical Example
- Mastering Java's String compareTo() Method: Syntax, Use Cases, and Practical Examples
- Java String.contains() Method: How to Check for Substrings – Practical Examples
- Polymorphism in Java: A Comprehensive Guide with Practical Examples
- Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
- Mastering Java's split() Method: A Practical Guide with Code Examples