Home >>Java String Methods >Java String contains() method

Java String contains() method

Java String contains()

The contains() method java string is used to checks the character sequence in that string. If sequence of char values is contained in this string otherwise returns false, it returns true.

Internal implementation


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

Signature

public boolean contains(CharSequence sequence)

Parameter

Sequence : Specifies the character series to be searched.

Returns

True if char value sequence exists, otherwise it is false.

Throws

NullPointerException : if sequence is null.

String contains() method example1

class ContainsExample
{  
public static void main(String args[])
{  
String nm="do you know phptpoint";  
System.out.println(nm.contains("do you know"));  
System.out.println(nm.contains("about"));  
System.out.println(nm.contains("phptpoint world"));  
}}  

Output
true
false
false
String contains() Method Example 2

public class ContainsExample2 
{  
    public static void main(String[] args) 
	{  
        String strl1 = "Hello Phptpoint Users";  
        boolean isContains = strl1.contains("Phptpoint");  
        System.out.println(isContains);  
        System.out.println(strl1.contains("Phptpoint"));   
    }  
} 

Output
True
False
String contains() Method Example 3

The method contain() is useful in finding a char-sequence in the string. We can use it to generate search based results in a control structure.


public class ContainsExample3 {   
    public static void main(String[] args) {          
        String strl2 = " Welcome to Phptpoint.com";        
        if(strl2.contains("Phptpoint.com")) {  
            System.out.println("Visit Phptpoint.com");  
        }else  
            System.out.println("Result not found");       
    }  
}  

Output
Visit Phptpoint.com