新手问题 清除空格有什么好办法吗?

metal · 2012年12月06日 · 最后由 metal 回复于 2012年12月06日 · 6824 次阅读
"    Hello      world     ". strip

只能把两边给清理掉了。 想要清理所有的空格我又想保持好单词的格式。

自己写个正则试试?

" Hello world ".gsub(/\s/, '')

" hello world ".gsub(/\s+/, ' ').strip

或者" hello world ".strip.gsub(/\s+/, ' ')

5 楼 已删除

rails 里面可以用 " Hello world ".squish

#6 楼 @zlx_star 哈哈哈。谢谢。我马上去看 Rails 的这段源代码。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号