Java String contains()

The contains() method checks whether the specified string (sequence of characters) is present in the string or not.

Example


Syntax of contains()

The syntax of the String contains() method is:

string.contains(CharSequence ch)

Here, string is an object of the String class.


contains() Parameters

The contains() method takes a single parameter.

  • ch (charSequence) - a sequence of characters

Note: A charSequence is a sequence of characters such as: String, CharBuffer, StringBuffer etc.


contains() Return Value

  • returns true if the string contains the specified character
  • returns false if the string doesn't contain the specified character

Example 1: Java String contains()

Here, str.contains("") gives true because the empty string is a subset of every other string.


Example 2: Using contains() With if...else

Output

Learn Java contains Java
Learn Java doesn't contain java