Java 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. Let’s see how does Java string contains() method works?
The return type of String contains() is boolean. It returns only true or false value.

Java string contains
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.

Example of Java string contains()

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

Output:
true
false
true
true

1 thought on “Java string contains”

Leave a Comment