Java String contains Method

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

In this article, we will show how to write a string contains in Java with multiple examples. 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

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

// Java String.contains example
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

Within this String contains example, we declared two String variables Str, Str1, and assigned corresponding values using the following statement.

String str = "Tutorials On Java Programming";
String str1 = "We are abc working at abc company";

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 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

boolean e = str1.contains("xyz");

Following Java System.out.println statements will print the output

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 Contains Example

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

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: 
Java Programming
Please type any word related to gate

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

System.out.println("Please Enter any word: ");
String str = sc.nextLine();

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

  • If the condition inside the If 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.