Java Tutorial Java Advanced Java References

Java String - contains() Method



The Java 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 example below, 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