String contains() Method

String contains() Method in Java with Example

What is contains()method in Java?

The contains() method is Java method to check if String contains another substring or not. It returns boolean value so it can use directly inside if statements.

Syntax of String “Contain” method


s − This is the sequence to search

Return Value

This method returns true only if this string contains “s” else false.


NullPointerException − if the value of s is null.


Contains sequence ‘ing’: true
Contains sequence ‘Example’: true
Contains sequence ‘example’: false
Contains sequence ‘is String’: false

When to use Contains() method?

It is a common case in programming when you want to check if specific String contains a particular substring. For example, If you want to test if the String “The big red fox” contains the substring “red.” This method is useful in such situation.

Example 2: Java String contains() method in the if else Loop