Java – Check if a String Ends with Specified Suffix
To check if a String str ends with a specific suffix string suffix in Java, use String.endsWith() method. Call endsWith() method on the string str and pass the suffix string suffix as argument. If str ends with the suffix string suffix, then endsWith() method returns true.
Java Program
</>
Copy
public class Example {
public static void main(String[] args) {
String str = "apple";
String suffix = "le";
boolean result = str.endsWith(suffix);
System.out.println("Does str end with specified suffix? " + result);
}
}
Output
Does str end with specified suffix? true
If str does not end with the suffix string suffix, then endsWith() method returns false.
Java Program
</>
Copy
public class Example {
public static void main(String[] args) {
String str = "apple";
String suffix = "pp";
boolean result = str.endsWith(suffix);
System.out.println("Does str end with specified suffix? " + result);
}
}
Output
Does str end with specified suffix? false
Conclusion
In this Java Tutorial, we learned how to check if a string ends with a specified suffix string in Java.
