
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do we check if a String contains a substring (ignoring case) in Java?
Problem Statement
The task is, given a string and a substring, you have to check if a substring is present in a string or not. Do not mind if the case of the string and substring is different, it should not matter.
For example, if the string is "Hello World" and the substring is "hello", then the output should be true. Even if the substring is "HELLO" or "HeLLo", the output should be true.
Solution
To solve this problem, we will first convert the string and the substring to lowercase using the toLowerCase()
method. The toLowerCase () method of the String class converts all the characters in the current String into lower case and returns.
Then we will use the contains()
Method of the String class to check if the substring is present in the string or not. The contains() method of the String class accepts a String value as a parameter, verifies whether the current String object contains the specified String, and returns true if it does (else false).
Let's look at the steps:
- Get the string.
- Get the Sub String.
- Convert the string value into lower case letters using the toLowerCase() method, store it as fileContents.
- Convert the substring value into lower case letters using the toLowerCase() method, store it as subString.
- Invoke the contains() method on the fileContents by passing the substring as a parameter to it.
Example 1
Following is the Java program to check if a string contains a substring ignoring case:
public class CheckIfSubstring{ public static void main(String[] args){ String str = "Hey, Welcome to the world of Java. Java is a programming language that is concurrent, class-based, and object-oriented. Java is designed to have as few implementation dependencies as possible."; String subStr = "welcome"; boolean result = str.toLowerCase().contains(subStr.toLowerCase()); if(result){ System.out.println("The string contains the substring."); }else{ System.out.println("The string does not contain the substring."); } } }
Output
Following is the output of the above code:
The string contains the substring.
Example 2
Here is another example, where the substring is not present in the string:
In this example, we will try to find the substring that is not present in the string.
public class CheckIfSubstring{ public static void main(String[] args){ String str = "Hey, Welcome to the world of Java. Java is a programming language that is concurrent, class-based, and object-oriented. Java is designed to have as few implementation dependencies as possible."; String subStr = "Python"; boolean result = str.toLowerCase().contains(subStr.toLowerCase()); if(result){ System.out.println("The string contains the substring."); }else{ System.out.println("The string does not contain the substring."); } } }
Output
Following is the output of the above code:
The string does not contain the substring.