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

metal · December 06, 2012 · Last by metal replied at December 06, 2012 · 6826 hits
"    Hello      world     ". strip

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

自己写个正则试试?

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

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

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

5 Floor has deleted

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

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

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