新手问题 怎么判断 params 传递进来的值为空格

wxliuzifan · February 02, 2015 · Last by jasl replied at February 02, 2015 · 1797 hits

如题。。。求大神

== " " 不行?

' '.blank? # true

#2 楼 @flowerwrong blank? 不光能匹配空格的,沒東西也能匹配

如果樓主指的是不定數目的空格,就用 /^\s+$/ 去 match 吧

#3 楼 @blacktulip ''.emtpy? # true ' '.empty? # false这样到是麻烦了。还是正则表达式吧

params[:x].empty? == false && params[:x].blank? == true

#5 楼 @flowerwrong 解决了。 你好,好淫

换个思路呢?去掉空格再判断咯~ params[:x].gsub(' ', '').blank?

#8 楼 @jasl 一樣,處理不了「沒東西」的情況

''.gsub(' ', '').blank?
 => true

#9 楼 @blacktulip 哦 看错问题了。。。

You need to Sign in before reply, if you don't have an account, please Sign up first.