Java Program to check if a string contains a substring
What are Strings?
A string is a sequence of characters. In Java, strings are used to represent text. You can use strings to store messages, names, and other kinds of text data.
Strings are usually represented as a series of characters enclosed in quotation marks. For example, in Java, you can create a string like this:
String s = “Hello World”;
In this Article, we will write a program to check if a string contains a substring.
Strings:
We can use the various methods of the String class to manipulate strings.
For example, you can use the length() method to get the length of a string, the substring() method to get a substring of a string, and the toUpperCase() method to convert a string to uppercase. You can also use the + operator or the concat() method to concatenate two strings.
What are SubString?
A substring is a contiguous sequence of characters within a string. For example, “Hello” is a string, and “ell” and “o” are substrings of that string. We can use built-in string functions of java to extract substrings from a larger string, or we can also use our own custom code to do extract the substring. Substrings are useful for a variety of tasks, such as extracting specific pieces of data from a larger string, searching for patterns within a string, and so on.
Program to check if a string contains a substring :
Here is an example of a Java program that prompts the user to enter a string and a substring, and then uses the contains() method to check if the string contains the substring:
import java.util.*; public class Main{ public static void main(String[] args) { Scanner scn = new Scanner(System.in); // Taking input for the String System.out.print("Enter a string: "); String str = scn.nextLine(); // Taking input for the SubString System.out.print("Enter a substring: "); String sub = scn.nextLine(); // Checking whether the substring is a part of the string or not if (str.contains(sub)) { // if the returned value is true System.out.println("The string contains the substring."); } else { // if the returned value is false System.out.println("The string does not contain the substring."); } } }
Output: Enter a string: Lakshit from PrepInsta Enter a substring: Lakshit The string contains the substring.
This code will prompt the user to enter a string and a substring, and then it will use the contains() method to check if the string contains the substring. If it does, it will print “The string contains the substring.” Otherwise, it will print “The string does not contain the substring.”
Note that the contains() method is case-sensitive, so “Hello” and “hello” are considered to be different substrings. If you want to ignore the case, you can convert both the string and the substring to lowercase or uppercase before checking if the string contains the substring.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment