Check whether a character sequence is present in a String

contains(CharSequence cs) method of String class can be used to check whether a character or word or sentence is contained in a string.

This method returns true if the character sequence is present in the string and false if character sequence is not present in the String. i.e return type is boolean.


Example 1

class StringTest{

public static void main(String args[]){

String str1=new String("ProgramZools is a developer's site");

boolean ispresent=str1.contains("developer");

if(ispresent==true){

System.out.println("'developer' is present in the string");

}

else{

System.out.println("'developer' is not present in the string");

}

}

}

Output 1

'developer' is present in the string


Example 2

class StringTest{

public static void main(String args[]){

String str1=new String("ProgramZools is a developer's site");

String str2="123";

if(str1.contains(str2)==true){

System.out.println("'123' is present in the string");

}

else{

System.out.println("'123' is not present in the string");

}

}

}

Output 2

'123' is not present in the string