Facebook Page Twitter Page LinkedIn Page
× Java Tutorial Java Advanced Java Resources


The Java string contains() method is used to search the specified character sequence in the given string. It returns true if the string contains the specified character sequence, else returns false.

Syntax

public boolean contains(CharSequence s)

Parameters

s specify the sequence to search for.

Return Value

Returns true if the string contains the specified character sequence, else returns false.

Exception

NA.

Example:

In the below example, contains() method is used to search the specified character sequence in the given string.

public class MyClass {
  public static void main(String[] args) {
    String MyString = "Learning Java is fun.";

    System.out.println(MyString.contains("Learn"));  
    System.out.println(MyString.contains("Yearn")); 
  }
}

The output of the above code will be:

true
false

❮ Java String Methods