- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
In this article, we will understand how to check if a string contains a substring. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). A part or a subset of string is called substring.
Below is a demonstration of the same −
Suppose our input is −
The first substring is defined as: Java The second substring is defined as: C++
The desired output would be −
The substring: Java is a part of the defined string. The substring: C++ is not a part of the defined string.
Algorithm
Step 1 - START Step 2 - Declare three string namely input_string, sub_string_1, sub_string_2 Step 3 - Define the values. Step 4 - Use the function .contains() to check if the string contains the substring. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
public class Demo { public static void main(String[] args) { String input_string = "Java Programming"; System.out.println("The input string is defined as: " +input_string); String sub_string_1 = "Java"; System.out.println("The first substring is defined as: " +sub_string_1); String sub_string_2 = "C++"; System.out.println("The second substring is defined as: " +sub_string_2); boolean result = input_string.contains(sub_string_1); if(result) { System.out.println("The substring: " +sub_string_1 + " is a part of the defined string."); } else { System.out.println("The substring: " +sub_string_1 + " is not a part of the defined string."); } result = input_string.contains(sub_string_2); if(result) { System.out.println("The substring: " +sub_string_2 + " is a part of the defined string."); } else { System.out.println("The substring: " +sub_string_2 + " is not a part of the defined string."); } } }
Output
The input string is defined as: Java Programming The first substring is defined as: Java The second substring is defined as: C++ The substring: Java is a part of the defined string. The substring: C++ is not a part of the defined string.
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
public class Demo { static void check_substring(String input_string, String sub_string_1, String sub_string_2){ boolean result = input_string.contains(sub_string_1); if(result) { System.out.println("The substring: " +sub_string_1 + " is a part of the defined string."); } else { System.out.println("The substring: " +sub_string_1 + " is not a part of the defined string."); } result = input_string.contains(sub_string_2); if(result) { System.out.println("The substring: " +sub_string_2 + " is a part of the defined string."); }else { System.out.println("The substring: " +sub_string_2 + " is not a part of the defined string."); } } public static void main(String[] args) { String input_string = "Java Programming"; System.out.println("The input string is defined as: " +input_string); String sub_string_1 = "Java"; System.out.println("The first substring is defined as: " +sub_string_1); String sub_string_2 = "C++"; System.out.println("The second substring is defined as: " +sub_string_2); check_substring(input_string, sub_string_1, sub_string_2); } }
Output
The input string is defined as: Java Programming The first substring is defined as: Java The second substring is defined as: C++ The substring: Java is a part of the defined string. The substring: C++ is not a part of the defined string.
- Related Articles
- How to check if a string contains a substring in Golang?
- PHP 8 – Using str_contains() to check if a string contains a substring
- How do we check if a String contains a substring (ignoring case) in Java?
- Java program to check if a Substring is present in a given String
- Java program to check if a string contains any special character
- How to check if an input string contains a specified substring in JSP?
- How to check whether a string contains a substring in jQuery?
- How to check whether a string contains a substring in JavaScript?
- How to check whether a String contains a substring or not?
- MySQL query to check if a string contains a value (substring) within the same row?
- Check if a string contains a number using Java.
- Java Regular expression to check if a string contains alphabet
- Java Program to check if the String contains only certain characters
- Python Program to check if a substring is present in a given string.
- C# program to check if a Substring is present in a Given String

Advertisements