ruby中有没有像java中的String的trim那样忽略空格的方法呢,答案当然是有的,查了下ruby API和google了下!
lstrip : 去掉首空格
rstrip : 去掉尾空格
gsub : 去掉全部空格,不过要用到pattern匹配
eg:
s1 = " Test whitespace" s2 = " hello Ruby Rails " s3 = "trailing " puts s1.lstrip+s3 # show lstrip remove leading whitespace puts s2.rstrip+s3 # show rstrip remove trail whitespace puts s1.strip + s2.strip + s3 # show strip remove leading and trail whitespace puts s2.gsub(/\s+/,'') puts s2.gsub(' ','') puts s2.gsub!(' ','') puts s2.gsub(//,'') # four methods show remove all whitespace |
#ref : http://railsforum.com/viewtopic.php?id=15030
#ref : http://www.nabble.com/remove-all-whitespaces-in-a-string-td10835684.html#a10835684