Java String contains Method

The Java contains method is one of the available String Methods. This contains function checks whether the string contains the user-specified character sequence (sequence of characters) or not. Based on the result, it will return Boolean True or False.

This article will show how to write a contains method with multiple examples. The basic syntax of the string contains in Java Programming language is as shown below.

Java String contains syntax

The following Java contains method accepts the Character Sequence as the argument and checks 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

In this Java program, we are going to use the contains method to check whether the string contains the given characters (in sequence) or not.

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

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

Let us try the wrong value for e. The following Java 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.

package StringFunctions;

public class ContainsMethod {
 public static void main(String[] args) {
 String str = "Tutorials On Java Programming";
 String str1 = "We are abc working at abc company";
 
 boolean a = str.contains("ori");
 boolean b = str.contains("On Java");
 boolean c = str1.contains("working");
 boolean d = str1.contains("abc");
 boolean e = str1.contains("xyz");
 
 System.out.println("Does the String str contains ori? = " + a);
 System.out.println("Does the String str contains On Java? = " + b);
 System.out.println("Does the String str1 contains working? = " + c);
 System.out.println("Does the String str1 contains abc? = " + d);
 System.out.println("Does the String str1 contains xyz? = " + e);
 }
}
Java String contains Method 1

Java String contains Method on user entered text

In this program, we are going to ask the user to insert any word. Based on the value entered by the user, the Java string contains method will display the message.

Within this String Method example, the first statement will ask the user to enter any word. Then we are going to assign the user entered value to the Java variable str.

Next, we used the If Else Statement to check whether the user entered string contains the character sequence “gate” or not.

  • If the condition inside the If the statement is True, the System.out.println(“Hey!! Welcome to Tutorial Gateway”); statement will print
  • Else System.out.println(“Please type any word related to gate”); statement printed.
package StringFunctions;

import java.util.Scanner;

public class ContainsMethodex {
 private static Scanner sc;
 public static void main(String[] args) {
 sc = new Scanner(System.in);
 
 System.out.println("Please Enter any word: ");
 String str = sc.nextLine();
 
 if (str.contains("gate")) {
 System.out.println("Hey!! Welcome to Tutorial Gateway");
 }
 else {
 System.out.println("Please type any word related to gate");
 }
 }
}
Please Enter any word: 
payment gateway
Hey!! Welcome to Tutorial Gateway

Let us enter a different word.

Please Enter any word: 
rogramming
Please type any word related to gate