Check whether a string ends with a character sequence in Java
endsWith(String suffix) method of String class can be used to check whether a String ends with a specific character sequence.
It returns true if the String ends with the given character sequence and returns false if the String does not end with given character sequence. i.e return type of the method is boolean.
Example 1
class StringTest{
public static void main(String args[]){
String str="You are wonderful";
boolean isOk=str.endsWith("wonderful");
if(isOk==true){
System.out.println("String ends with 'wonderful'");
}
else{
System.out.println("String does not end with 'wonderful'");
}
}
}
Output 1
String ends with 'wonderful'
Example 2
class StringTest{
public static void main(String args[]){
String str="You are wonderful";
boolean isOk=str.endsWith("You");
if(isOk==true){
System.out.println("String ends with 'wonderful'");
}
else{
System.out.println("String does not end with 'wonderful'");
}
}
}
Output 2
String does not end with 'wonderful'