Java Program to Check if a string contains a substring


A string is a class in Java that stores a series of characters enclosed within double quotes and a continuous sequence of characters within that string is termed as substring. Those characters are actually String-type objects. This article aims to write Java programs to check if a string contains a substring or not. To check whether the given string contains a substring or not, we can use indexOf(), contains() and substring() methods along with conditional blocks.

Java Program to Check if a String Contains a Substring

We are going to use the following built-in methods in our Java programs to check if a string contains a substring −

  • indexOf()

  • contains()

  • substring()

Let's discuss these methods one by one but before that, it is necessary to understand the problem statement with an example.

Instance

Input

String = "Simply Easy Learning";
Substring = "Easy";

Output

The string contains the given substring

In the above example, the substring “Easy” is contained in the given string “Simply Easy Learning”. Hence, we are getting the output message as “The string contains the given substring”.

Now, let's discuss the Java programs to check whether the string contains the given substring or not.

Using indexOf() method

The indexOf() method of String class is used to find the position of spefcified substring within the given string. It returns the index of the first occurrence of that substring and -1 if the substring is not found.

Syntax

String.indexOf(subString);

Example  

The following example illustrates how to use the indexOf() method to check if a string contains the given substring or not.

public class Example1 {
   public static void main(String []args) {
      String inputStr = "Simply Easy Learning";
      // Substring to be checked
      String subStr = "Easy";
      System.out.println("The given String: " + inputStr);
      System.out.println("The given Substring: " + subStr);
      // checking the index of substring
      int index = inputStr.indexOf(subStr);
      // to check string contains the substring or not
      if (index != -1) {
         System.out.println("The string contains the given substring");
      } else {
         System.out.println("The string does not contain the given substring");
      }
   }
}

Output

The given String: Simply Easy Learning
The given Substring: Easy
The string contains the given substring

Using contains() method

The contains() method is also a built-in method of String class which is used to identify if a string contains the given substring or not. Its return type is Boolean which means it returns true if the substring is available within the string otherwise false.

Example  

In this example, we will use the built-in method contains() to check if a string contains the given substring or not.

public class Example2 {
   public static void main(String []args) {
      String inputStr = "Simply Easy Learning";
      // Substring to be checked
      String subStr = "Simply";
      System.out.println("The given String: " + inputStr);
      System.out.println("The given Substring: " + subStr);
      // to check string contains the substring or not
      if (inputStr.contains(subStr)) {
         System.out.println("The string contains the given substring");
      } else {
         System.out.println("The string does not contain the given substring");
      }
   }
}

Output

The given String: Simply Easy Learning
The given Substring: Simply
The string contains the given substring

Using substring() method

This is another method of String class which is used to print a substring from a given string. It accepts the beginning and ending index as arguments and returns the characters available in between those indices.

Example  

In the following example, we will use the substring() method to find a substring from index 0 to 2 of the given string.

public class Example3 {
   public static void main(String []args) {
      // initializing the string 
      String inputStr = "Simply Easy Learning";
      System.out.println("The given String is: " + inputStr);
      // Creating a Substring
      String subStr = inputStr.substring(0, 2);
      // printing one of the substring of the given string
      System.out.println("One substring of the given String: " + subStr);
   }
}

Output

The given String is: Simply Easy Learning
One substring of the given String: Si

Conclusion

In this article, we have learned about what is string and substring and also, how we can check if a string contains the given substring. To check whether the given substring is available in a specified string or not, we have used the built-in methods named indexOf(), contains() and substring() of the Java String class.

Updated on: 10-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements