Java String contains Method

The Java contains method is one of the String Method that is viable in Java. This string contains function is used to check whether the string contains the user specified character sequence (sequence of characters) or not, and based on the result it will return Boolean True or False.

In this article we will show you, How to write string contains in Java with multiple example. Before we get into the example, let us see the basic syntax of the string.contains in Java Programming language is as shown below.

Java contains syntax

The following java string contains function accepts the Character Sequence as the argument and check whether the string contains the user given sequence of characters or not

public boolean contains(CharSequence seq); // It will return boolean True or False
//In order to use in program
String_Object.contains(CharSequence seq)

Java String contains Method Example

The Java string contains method is used to check whether the string contains the given characters (in sequence) or not. In this Java program, We are going to find the same.

The following statement calls public boolean contains (CharSequence seq) method to check whether the string str contains the character sequence “ori” or not. If it is TRUE, it will return TRUE otherwise False

boolean a = str.contains("ori");

The following Java String contains statement will check whether the string str contains the char sequence On Java or not. If it is TRUE, it will return TRUE otherwise False

boolean b = str.contains("On Java");

Let us try the wrong value. The following statement will call the boolean contains (CharSequence seq) function to check whether the string str1 contains the character sequence xyz or not. We all know that, it is False