随笔-167  评论-65  文章-0  trackbacks-0

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



write by feng
posted on 2009-04-02 16:03 fl1429 阅读(6302) 评论(0)  编辑  收藏 所属分类: Rails

只有注册用户登录后才能发表评论。


网站导航:
 
已访问数:
free counters