Home »
Ruby Tutorial »
Ruby Programs
Ruby program to check the given string is empty or not
Last Updated : December 15, 2025
Problem Solution
In this program, we will create two string variables and check created string is empty or not using the empty?() method and print the appropriate message. The empty?() method is used to check a string is empty or not. It returns true, if the string is empty otherwise it returns false.
Program/Source Code
The source code to check given string is empty or not is given below. The given program is compiled and executed successfully.
# Ruby program to check given the
# string is empty or not
Str1 = "Hello";
Str2 = "";
if Str1.empty?()
print "Str1 is empty\n";
else
print "Str1 is not empty\n";
end
if Str2.empty?()
print "Str2 is empty\n";
else
print "Str2 is not empty\n";
end
Output
Str1 is not empty
Str2 is empty
Explanation
In the above program, we created 2 string variables Str1, Str2 that are initialized with "Hello", "". Then we used empty?() method to check created strings are empty or not and print the appropriate message.
Ruby Strings Programs »
Advertisement
Advertisement