इस लेख में, हम समझेंगे कि कैसे जांचा जाए कि किसी स्ट्रिंग में सबस्ट्रिंग है या नहीं। स्ट्रिंग एक डेटाटाइप है जिसमें एक या अधिक वर्ण होते हैं और दोहरे उद्धरण चिह्नों ("") में संलग्न होते हैं। स्ट्रिंग के एक भाग या उपसमुच्चय को सबस्ट्रिंग कहा जाता है।
नीचे उसी का एक प्रदर्शन है -
मान लीजिए कि हमारा इनपुट है -
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.
एल्गोरिदम
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
उदाहरण 1
यहां, हम 'मेन' फंक्शन के तहत सभी ऑपरेशंस को एक साथ बांधते हैं।
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."); } } }
आउटपुट
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.
उदाहरण 2
यहां, हम ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग को प्रदर्शित करने वाले कार्यों में संचालन को समाहित करते हैं।
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); } }
आउटपुट
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.