5 - contains(), endsWith() Java String Methods

Java String Tutorial

Fri Jan 14, 2022

contains() and endsWith() Methods

contains()

contains() method searches the sequence of characters not char in the string. It returns true if the sequence of char is found in this string otherwise returns false.

Example: 

String S1="Welcome"; 

System.out.println(S1.contains("c")); 

System.out.println(S1.contains("elc")); 

System.out.println(S1.contains("elo")); 

Output: 

true 

true 

false


endsWith() 

endsWith() method checks if this string ends with a given suffix. It returns true if this string ends with the given suffix; else returns false. 

String S1="Welcome"; 

System.out.println(S1.endsWith("e")); 

System.out.println(S1.endsWith("m")); 

System.out.println(S1.endsWith("me")); 

Output: 

true 

false 

true

S P SHARMA CLASSES
S P SHARMA SIR

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
S P SHARMA CLASSES 2024 Privacy policy Terms of use Contact us Refund policy