Java String endsWith() method
Checks whether this String ends with the specified suffix or not. Syntax
public boolean endsWith(String Suffix)Parameter Suffix : It takes suffix as a parameter i.e. Sequence of characters Returns It returns true when the current String is matched with the given suffix. Otherwise, it returns false Java String endsWith() Example 1:
public class JavaStringEndsWithEx1 { public static void main(String args[]) { String str1,str2; str1="welcome to java tutorial"; str2=str1.concat(" and example"); System.out.println(str2.endsWith("ample")); System.out.println(str1.endsWith("al")); System.out.println(str1.endsWith("exampale")); } }Output:
true true falseJava String endsWith() Example 2:
public class JavaStringEndsWithEx2 { public static void main(String[] args) { String str = "tutorialandexample"; System.out.println(str.endsWith("and")); if(str.endsWith("example")) { String str2=str.concat(".com"); System.out.println(str2); }else System.out.println("something else"); } }Output:
false tutorialandexample.comJava String endsWith() Example 3:
public class JavaStringEndsWithEx3 { public static void main(String[] args) { String[] s1 = {"welcome","to","java","tutorial"}; System.out.println(s1[0]); System.out.println(s1[1]); //retriveing the array strings for (String element:s1){ if ( element.endsWith("a")) { System.out.println("java"); }else if( element.endsWith("l")){ System.out.println("tutorial"); } } } }Output:
welcome to java tutorial