Home >>Java String Methods >Java String contains() method
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.
public boolean contains(CharSequence s)
{
return indexOf(s.toString()) > -1;
}
public boolean contains(CharSequence sequence)
Sequence : Specifies the character series to be searched.
True if char value sequence exists, otherwise it is false.
NullPointerException : if sequence is null.
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"));
}}
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"));
}
}
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");
}
}