String contains()

This method is used to search the sequence of characters in a string. It means you can search for any number of characters. Even you can search for a single character or substring. It returns true if a sequence of characters is found otherwise false. The return type of contains() is boolean. It returns only a true or false value.

stringName.contains(charSequence)

stringName: The name of the string in which you want to search the given character sequence.
charSequence: It is a sequence of characters. Basically, CharSequence is an interface that is implemented by String class.

class ExampleOfContains
{  
    public static void main(String args[])
    {    
 	String string1 = "JAVA WINGS"; 
        // It will returns true 
	System.out.println(string1.contains("WINGS")); 
        // It will returns false because JAVA is case sensitive.
	System.out.println(string1.contains("wings")); 
        // It will returns true
	System.out.println(string1.contains("ING")); 
        // It will returns true 
	System.out.println(string1.contains("W")); 
	}
}  

Output:
true
false
true
true

Leave a Comment