
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
Java Program to Check if a string contains a substring
A string is a class in Java that stores a series of characters enclosed within double quotes, and a continuous sequence of characters within that string is known as a substring.
Those characters are string-type objects. In this article, we will learn how to write Java programs to check if a string contains a substring or not.
Java Program to Check if a String Contains a Substring
We can check whether the given string contains a substring or not in the following ways:
- Using indexOf() Method
- Using contains() Method
- Using Nested Loops
Let's discuss these methods one by one, but before that, it is necessary to understand the problem statement with an example.
In the below example, the substring "Easy" is contained in the given string "Simply Easy Learning". Hence, we are getting the output message as "The string contains the given substring".
Input String = "Simply Easy Learning"; Substring = "Easy"; Output The string contains the given substring
Now, let's discuss the Java programs to check whether the string contains the given substring or not.
Using indexOf() Method
The indexOf() method of the String class is used to find the position of a specified substring within the given string. It returns the index of the first occurrence of that substring and -1 if the substring is not found.
Example
In the following example, we will check if a string contains a substring using the indexOf() method.
public class CheckSubstring { public static void main(String[] args) { String str = "Simply Easy Learning"; String subStr = "Easy"; if (str.indexOf(subStr) != -1) { System.out.println("The string contains the given substring"); } else { System.out.println("The string does not contain the given substring"); } } }
Following is the output of the above code:
The string contains the given substring
Using contains() Method
The contains() method is also a built-in method of the String class, which is used to identify if a string contains the given substring or not. Its return type is Boolean, which means it returns true if the substring is available within the string; otherwise, false.
Example
In this example, we will use the built-in method contains() to check if a string contains the given substring or not.
public class CheckSubstring { public static void main(String[] args) { String str = "Simply Easy Learning"; String subStr = "Easy"; if (str.contains(subStr)) { System.out.println("The string contains the given substring"); } else { System.out.println("The string does not contain the given substring"); } } }
Following is the output of the above code:
The string contains the given substring
Using Nested Loops
We can also check if a string contains a substring by using nested loops. To do so, we need to iterate through each character of the string and compare it with the characters of the substring.
Example
In the following example, we will check if a string contains a substring using nested loops:
public class CheckSubstring { public static void main(String[] args) { String str = "Simply Easy Learning"; String subStr = "Easy"; boolean found = false; for (int i = 0; i <= str.length() - subStr.length(); i++) { int j; for (j = 0; j < subStr.length(); j++) { if (str.charAt(i + j) != subStr.charAt(j)) { break; } } if (j == subStr.length()) { found = true; break; } } if (found) { System.out.println("The string contains the given substring"); } else { System.out.println("The string does not contain the given substring"); } } }
Following is the output of the above code:
The string contains the given substring