美文网首页
ruby-去除空格

ruby-去除空格

作者: GALAXY_ZMY | 来源:发表于2015-09-28 22:38 被阅读417次

lstrip : 去掉首空格
rstrip : 去掉尾空格
gsub : 去掉全部空格,不过要用到pattern匹配

a = " hello world "
puts a.lstrip => "hello world "
puts a.rstrip => " hello world"
puts a.lstrip.rstrip => "hello world"
puts a.gsub(/\s+/,'') => "helloworld"
puts a.gsub(' ','') => "helloworld"
puts a.gsub(//,'') => "helloworld"

写了多个空格,传上去就成一个空格了。

相关文章

网友评论

      本文标题:ruby-去除空格

      本文链接:https://www.haomeiwen.com/subject/ivbwcttx.html