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 »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.