Ruby 检查字符串是否为空的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35917560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby way to check if a string is not blank?
提问by Rajkaran Mishra
What's the best way to check if a variable is not blank in an else if condition in Ruby (not Rails)?
检查 Ruby(不是 Rails)中 else if 条件中的变量是否为空的最佳方法是什么?
elsif not variable.to_s.empty?
# do something
end
or
或者
elsif !variable.to_s.empty?
# do something
end
or
或者
elsif variable.to_s.length > 0
# do something
end
回答by Lukas Baliak
string = ""
unless string.to_s.strip.empty?
# ...
end
回答by RamanSM
I just found out that ''.empty?returns truebut ' '.empty?returns false. Even to_s.lengthfor ' 'is not zero.
我刚刚发现''.empty?返回true但' '.empty?返回false。即使to_s.lengthfor' '不是零。
Maybe it is better to use strip as ' '.strip.empty?
也许最好使用 strip 作为 ' '.strip.empty?
回答by three
You can use either
你可以使用
unless var.empty?
#do sth
end
or
或者
unless var == ""
#do sth
end
or all of these with ifand a negator !.
或所有这些都带有if和一个否定词!。
回答by lcjury
The source of the empty?method is analogous to the following:
该empty?方法的来源类似于以下内容:
def empty?
return length == 0
end
So, you can safely use
因此,您可以安全地使用
any_string.length != 0
Anyway, using that code inside an else if is a bit verbose, I would encourage you to define the present?method inside the Stringclass.
无论如何,在 else if 中使用该代码有点冗长,我鼓励您present?在String类中定义该方法。
class String
def present?
!empty?
end
end
Now you can write your code the following way:
现在您可以按以下方式编写代码:
if some_condition
# do something
elsif variable.to_s.present?
# do something else
end
This way you get a clear code, without using negationsor unlesswho are hard to read.
这样你就可以得到一个清晰的代码,无需使用negations或unless难以阅读。
Of course, there is one problem here, I took the present?name (and method) from Rails. present?returns true if the object is not blank, but strings with tabs or spaces (white characters) are considered blanks. So, this present?will return true to for the following strings:
当然,这里有一个问题,我present?从 Rails 中取了名字(和方法)。 present?如果对象不是空白,则返回 true,但带有制表符或空格(白色字符)的字符串被视为空白。因此,present?对于以下字符串,这将返回 true :
"".present? # => false
" ".present? # => true
"\t\n\r".present? # => true
" blah ".present? # => true
It depends on what you want, high chances are that you want to get true for the first 3 strings, and false for the later. You could use @RamanSM approach and use stripto avoid empty spaces
这取决于您想要什么,很可能您希望前 3 个字符串为真,而后一个为假。您可以使用@RamanSM 方法并用于strip避免空格
class String
def present?
!strip.empty?
end
end
now, present?returns false for strings with white spaces
现在,present?对于带有空格的字符串返回 false
"".present? # => false
" ".present? # => false
"\t\n\r".present? # => false
" blah ".present? # => true
回答by Ashwini Kumar
For the string (say abc) which is not defined/undefined we should check for abc.nil?otherwise abc.blank? will throw (NoMethodError) undefined method empty?for nil:NilClass error
对于未定义/未定义的字符串(比如 abc),我们应该检查abc.nil?其他 abc.blank?将为empty?nil:NilClass 错误抛出 (NoMethodError) 未定义的方法

