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");
}
}
}
The main string contains the substring
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
. main string
contains the substring
. Otherwise, we print a message saying that the main string does not contain the substring
.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()
.