
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
Check if a Substring is Present in a Given String in Java
Suppose you are given two strings, your task is to write a Java program that checks whether the second string is the sub-string of the first one. String in Java is an immutable sequence of characters and sub-string is its small portion.
Example Scenario: ?
Input 1: str = "The sunset is beautiful"; Input 2: sub_str = "sunset"; Output: res = found!!
Using Iteration
In this approach, the idea is to use a nested for loop and an if block. The outer for loop will iterate over the characters of the main string. For each starting position i, the inner for loop will compare each character of the sub-string with the corresponding character in the main string using if block.
If any character does not match, the inner loop will break, and the outer loop will move to the next starting position. If the inner loop completes without breaking, it means the substring is found at the current starting position i. The code then prints the index.
Example
A Java program that demonstrates how to check if sub-string is present in a given string is as follows ?
public class Example { public static void main(String args[]) { String str = "Apples are red"; String substr = "are"; int n1 = str.length(); int n2 = substr.length(); System.out.println("String: " + str); System.out.println("Substring: " + substr); for (int i = 0; i <= n1 - n2; i++) { int j; for (j = 0; j < n2; j++) { if (str.charAt(i + j) != substr.charAt(j)) break; } if (j == n2) { System.out.println("The substring is present in the string at index " + i); return; } } System.out.println("The substring is not present in the string"); } }
The above code will display the following output ?
String: Apples are red Substring: are The substring is present in the string at index 7
Using contains() Method
The String class of java.lang package provides the contains() method which is used to check if a particular sequence of characters is present within a string or not.
Example
Let's see the practical implementation.
public class Example { public static void main(String[] args) { String str = "Apples are red"; String substr = "red"; // to check substring boolean checkSubStr = str.contains(substr); if (checkSubStr) { System.out.println(substr + " is present in the string"); } else { System.out.println(substr + " is not present in the string"); } } }
On running, this code will give following result ?
red is present in the string
Using indexOf() Method
This is another way to find out if a string contains a certain sub-string. Here, we use the indexOf() method of String class. It returns the index of the first occurrence of the specified sub-string. If the substring is not present it returns -1.
Example
In this Java program, we use indexOf() method to check if a sub-string is present in a given string.
public class Example { public static void main(String[] args) { String str = "Apples are red"; String substr = "ples"; // to check substring int checkSubStr = str.indexOf(substr); if (checkSubStr != -1) { System.out.println(substr + " is present in the string at index: " + checkSubStr); } else { System.out.println(substr + " is not present in the string"); } } }
Output of the above code is given below ?
ples is present in the string at index: 2