এই আর্টিকেলে আমরা বুঝব কীভাবে চেক করতে হয় একটি স্ট্রিং খালি (empty) নাকি নাল (null)। String হলো একটি ডেটা টাইপ যাতে এক বা একাধিক ক্যারেক্টার থাকে এবং ডবল কোটেশন (" ") দিয়ে ঘেরা থাকে।
ধরুন ইনপুট:
Input string: null
চাহিদা আউটপুট:
The string is a null string
অ্যালগরিদম
Step 1 - START Step 2 - একটি স্ট্রিং ডিক্লেয়ার করুন input_string নামে Step 3 - মান ডিফাইন Step 4 - if-লুপ ব্যবহার করে input_string == null কম্পিউট করুন। true হলে স্ট্রিং null, false হলে স্ট্রিং null নয় Step 5 - ফলাফল প্রদর্শন Step 6 - STOP
উদাহরণ ১
এখানে সকল অপারেশন '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
উদাহরণ ২
এখানে অপারেশনগুলো ফাংশনে এনক্যাপসুলেট করা হয়েছে অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিং প্রদর্শন করতে।
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