In Java, you can check whether a given string contains a substring or not using the contains()
method of the String
class.
The contains()
method returns true
if the specified substring is present in the given string, otherwise, it returns false
.
Here is a Java program that demonstrates how to use the contains()
method to check if a string contains a substring:
public class SubstringExample { public static void main(String[] args) { String str = "Hello, World!"; String substr1 = "World"; String substr2 = "Java"; boolean result1 = str.contains(substr1); boolean result2 = str.contains(substr2); System.out.println("Result 1: " + result1); System.out.println("Result 2: " + result2); } }
In the above program, we have defined a string str
and two substrings substr1
and substr2
.
We then use the contains()
method to check if str
contains substr1
and substr2
.
The results are stored in result1
and result2
variables and printed on the console.
If you run the above program, you will get the following output:
Result 1: true Result 2: false
As you can see, the contains()
method returns true
for substr1
because it is present in str
, and false
for substr2
because it is not present in str
.
In summary, checking if a string contains a substring is a common task in Java programming, and it can be easily done using the contains()
method of the String
class.
If you need to check for a substring case-insensitively, you can convert both the string and the substring to lowercase or uppercase using the toLowerCase()
or toUpperCase()
methods of the String
class, and then use the contains()
method to perform the check.
Here is an example program that demonstrates case-insensitive substring checking:
public class SubstringExample { public static void main(String[] args) { String str = "Hello, World!"; String substr1 = "worlD"; String substr2 = "Java"; boolean result1 = str.toLowerCase().contains(substr1.toLowerCase()); boolean result2 = str.toLowerCase().contains(substr2.toLowerCase()); System.out.println("Result 1: " + result1); System.out.println("Result 2: " + result2); } }
In the above program, we convert both str
and substr1
to lowercase using the toLowerCase()
method, and then use the contains()
method to check if str
contains substr1
case-insensitively.
The results are stored in result1
and result2
variables and printed on the console.
If you run the above program, you will get the following output:
Result 1: true Result 2: false
As you can see, the contains()
method performs a case-insensitive substring check and returns true
for substr1
because it is present in str
case-insensitively, and false
for substr2
because it is not present in str
.
In conclusion, checking if a string contains a substring is a straightforward task in Java, and it can be done using the contains()
method of the String
class.
If you need to perform a case-insensitive substring check, you can convert both the string and the substring to lowercase or uppercase using the toLowerCase()
or toUpperCase()
methods of the String
class, and then use the contains()
method to perform the check.