इस लेख में, हम समझेंगे कि स्ट्रिंग खाली है या नल कैसे जांचें। स्ट्रिंग एक डेटा टाइप है जिसमें एक या अधिक कैरेक्टर्स होते हैं और डबल कोट्स (" ") में संलग्न होते हैं।
नीचे इसका प्रदर्शन है:
मान लीजिए हमारा इनपुट है:
Input string: null
वांछित आउटपुट होगा:
The string is a null string
एल्गोरिदम
Step 1 - START Step 2 - एक स्ट्रिंग घोषित करें जिसे input_string कहते हैं। Step 3 - मान परिभाषित करें। Step 4 - if-लूप का उपयोग करके input_string == null की गणना करें। यदि सच है, तो स्ट्रिंग नल है, अन्यथा स्ट्रिंग नल नहीं है। Step 5 - परिणाम प्रदर्शित करें Step 6 - Stop
उदाहरण 1
यहां, हम सभी ऑपरेशन्स को 'main' फंक्शन के तहत एक साथ बांधते हैं।
public class Demo { public static void main(String[] args) { String input_string = null; System.out.println("The string is defined as: " + input_string); if (input_string == null) { System.out.println("\nThe string is a null string"); } else if(input_string.isEmpty()){ System.out.println("\nThe string is an empty string"); } else { System.out.println("\nThe string is neither empty nor null string"); } } }आउटपुट
The string is defined as: null The string is a null string
उदाहरण 2
यहां, हम ऑपरेशन्स को फंक्शन्स में एनकैप्सुलेट करते हैं जो ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग को दर्शाता है।
public class Demo { static void isNullEmpty(String input_string) { if (input_string == null) { System.out.println("\nThe string is a null string"); } else if(input_string.isEmpty()){ System.out.println("\nThe string is an empty string"); } else { System.out.println("\nThe string is neither empty nor null string"); } } public static void main(String[] args) { String input_string = null; System.out.println("The string is defined as: " + input_string); isNullEmpty(input_string); } }आउटपुट
The string is defined as: null The string is a null string