Ruby program to check the given string is empty or not

Ruby Example: Write a program to check the given string is empty or not.
Submitted by Nidhi, on January 01, 2022

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 »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.