Computer >> Computer tutorials >  >> Programming >> Java

Java Program to Check if a string contains a substring


In this article, we will understand how to check if a string contains a substring. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). A part or a subset of string is called substring.

Below is a demonstration of the same −

Suppose our input is

The first substring is defined as: Java
The second substring is defined as: C++

The desired output would be

The substring: Java is a part of the defined string.
The substring: C++ is not a part of the defined string.

Algorithm

Step 1 - START
Step 2 - Declare three string namely input_string, sub_string_1, sub_string_2
Step 3 - Define the values.
Step 4 - Use the function .contains() to check if the string contains the substring.
Step 5 - Display the result
Step 6 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

public class Demo {
   public static void main(String[] args) {
      String input_string = "Java Programming";
      System.out.println("The input string is defined as: " +input_string);
      String sub_string_1 = "Java";
      System.out.println("The first substring is defined as: " +sub_string_1);
      String sub_string_2 = "C++";
      System.out.println("The second substring is defined as: " +sub_string_2);
      boolean result = input_string.contains(sub_string_1);
      if(result) {
         System.out.println("The substring: " +sub_string_1 + " is a part of the defined string.");
      } else {
         System.out.println("The substring: " +sub_string_1 + " is not a part of the defined string.");
      }
      result = input_string.contains(sub_string_2);
      if(result) {
         System.out.println("The substring: " +sub_string_2 + " is a part of the defined string.");
      } else {
         System.out.println("The substring: " +sub_string_2 + " is not a part of the defined string.");
      }
   }
}

Output

The input string is defined as: Java Programming
The first substring is defined as: Java
The second substring is defined as: C++
The substring: Java is a part of the defined string.
The substring: C++ is not a part of the defined string.

Example 2

Here, we encapsulate the operations into functions exhibiting object-oriented programming.

public class Demo {
   static void check_substring(String input_string, String sub_string_1, String sub_string_2){
      boolean result = input_string.contains(sub_string_1);
      if(result) {
         System.out.println("The substring: " +sub_string_1 + " is a part of the defined string.");
      } else {
         System.out.println("The substring: " +sub_string_1 + " is not a part of the defined string.");
      }
      result = input_string.contains(sub_string_2);
      if(result) {
         System.out.println("The substring: " +sub_string_2 + " is a part of the defined string.");
      }else {
         System.out.println("The substring: " +sub_string_2 + " is not a part of the defined string.");
      }
   }
   public static void main(String[] args) {
      String input_string = "Java Programming";
      System.out.println("The input string is defined as: " +input_string);
      String sub_string_1 = "Java";
      System.out.println("The first substring is defined as: " +sub_string_1);
      String sub_string_2 = "C++";
      System.out.println("The second substring is defined as: " +sub_string_2);
      check_substring(input_string, sub_string_1, sub_string_2);
   }
}

Output

The input string is defined as: Java Programming
The first substring is defined as: Java
The second substring is defined as: C++
The substring: Java is a part of the defined string.
The substring: C++ is not a part of the defined string.