Java String contains() method

Java String contains() method checks whether a particular string is a part of another string or not. This method returns true if a specified sequence of characters is present in a given string, else it returns false.

Syntax of contains() method
public boolean contains(CharSequence str)

The return type is boolean which means this method returns true or false. When the character sequence found in the given string then this method returns true else it returns false.

If the CharSequence is null then this method throws NullPointerException.

For example: calling this method like this would throw NullPointerException.

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

The signature of string contains() method is given below

public boolean contains(CharSequence sequence)
Parameter

sequence : specifies the sequence of characters to be searched.

Returns

true if sequence of char value exists, otherwise false.

Throws

NullPointerException : if sequence is null.

Java String contains() method example
class Test1 { public static void main(String args[]) { String name="what do you know about me"; System.out.println(name.contains("do you know")); System.out.println(name.contains("about")); System.out.println(name.contains("hello")); } }


Output:
true true false
Java String contains() method Example

The second print statement displayed false because the contains() method is case sensitive. You can also use the contains() method for case insensitive check, I have covered this at the end of this tutorial.

class Test1 { public static void main(String args[]) { String str = "Do you like watching Game of Thrones"; System.out.println(str.contains("like")); /* this will print false as the contains() method is * case sensitive. Here we have mentioned letter "l" * in upper case and in the actual string we have this * letter in the lower case. */ System.out.println(str.contains("Like")); System.out.println(str.contains("Game")); System.out.println(str.contains("Game of")); } }


Output:
true false true true
Example: Using Java String contains() method in the if-else statement
class Test1 { public static void main(String args[]) { String str = "This is an example of contains()"; /* Using the contains() method in the if-else statements, since * this method returns the boolean value, it can be used * in the if else conditions whenever needed. */ if(str.contains("example")) { System.out.println("The word example is found in given string"); } else{ System.out.println("The word example is not found in the string"); } } }


Output:
The word example is found in given string
Java String contains() Method Example

The contains() method searches case sensitive char sequence. If the argument is not case sensitive, it returns false. Let's see an example below.

public class Test1 { public static void main(String[] args) { String str = "Hello c programming readers"; boolean isContains = str.contains("programming"); System.out.println(isContains); // Case Sensitive System.out.println(str.contains("c programming")); // false } }


Output:
true true
Java String contains() Method Example

The contains() method is helpful to find a char-sequence in the string. We can use it in control structure to produce search based result.

public class Test1 { public static void main(String[] args) { String str = "To learn Java visit cprogramcoding.com"; if(str.contains("cprogramcoding.com")) { System.out.println("This string contains cprogramcoding.com"); }else System.out.println("Result not found"); } }


Output:
This string contains cprogramcoding.com



Instagram