What is the method in Java for determining whether a string contains another string?

In Java, you can determine if a string contains a certain substring by using the contains() method from the String class. This method will return a boolean value indicating whether the string includes the specified substring.

For example:

String str = "Hello, World!";
boolean containsHello = str.contains("Hello"); // 判断str中是否包含"Hello"子字符串
if(containsHello) {
    System.out.println("包含Hello");
} else {
    System.out.println("不包含Hello");
}

In the above code, the variable containsHello will store the result of whether the string contains “Hello”. If it does, it will output “Contains Hello”; otherwise, it will output “Does not contain Hello”.

bannerAds