To check if a string is empty in Ruby, you can use the empty?
method.
In this example,
str1
with an empty string value ""
.empty?
method on str1
to check if it is empty.empty?
method returns true
if the string is empty, otherwise false
.str1 = ""
isEmpty = str1.empty?
puts "Is string empty: #{isEmpty}"
Is string empty: true
In this example,
str2
with a non-empty string value "Hello World"
.empty?
method on str2
to check if it is empty.empty?
method returns true
if the string is empty, otherwise false
.str2 = "Hello World"
isEmpty = str2.empty?
puts "Is string empty: #{isEmpty}"
Is string empty: false
In this tutorial, we learned How to Check if String is Empty in Ruby language with well detailed examples.