Q:

Ruby program to check the given string is empty or not

belongs to collection: Ruby Strings Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 Str1Str2 that are initialized with "Hello", "".  Then we used empty?() method to check created strings are empty or not and print the appropriate message.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Strings Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to demonstrate the string interpolati... >>
<< Ruby program to compare two strings by ignoring ca...