Java contains() method

    contains() method

    This method in java will allow you to search for a sequence of character within the string. The true value will be returned if the character sequence will be found in the string else the false will be returned. If the provided sequence is null then the NullPointerException will be returned.

    Method implementation-

    public boolean contains(CharSequence s) {  
    
          return indexOf(s.toString()) > -1;  
    
      }  

    Syntax-

    public boolean contains(CharSequence sequence)  

    Example-

    public class Simple{  
    
    public static void main(String[] args) {      
    
           String str="how are you man";  
    
           System.out.println(str.contains("how"));  
           System.out.println(str.contains("you"));  
          }
    } 

    Output-

    Example with case sensitive character-

    public class Simple{  
    
    public static void main(String[] args) {      
    
           String str="how are you man";  
    
    boolean val = str.contains("you");  
    
            System.out.println(val);   
    
            System.out.println(str.contains("HOW")); 
    
        }  
    
    }  

    Output-