Java Program to Check if a String Contains a Substring

In the following example of Java program to check if a string contains a substring :
Program :
public class SubstringDemo {
    public static void main(String[] args) {
        String mainString = "Hello World";
        String subString = "World";

        if (mainString.contains(subString)) {
            System.out.println("The main string contains the substring");
        } else {
            System.out.println("The main string does not contain the substring");
        }
    }
}
Output :
The main string contains the substring
We define a main string mainString with the value "Hello World" and a substring subString with the value "World". We then use the contains() method of the String class to check if mainString contains subString.

If it does, we print a message saying that the main string contains the substring. Otherwise, we print a message saying that the main string does not contain the substring.

Note that the contains() method is case-sensitive. If you want to perform a case-insensitive search, you can convert both the main string and the substring to lowercase or uppercase using the toLowerCase() or toUpperCase() method before calling contains().