删除全部空格的Ruby函数是什么? 有点像php的trim()
? php
s = "I have white space".delete(' ')
并模拟PHP的trim()
函数: api
s = " I have leading and trailing white space ".strip
相关回答: 框架
" clean up my edges ".strip
回报 函数
"clean up my edges"
也别忘了: ui
$ s = " I have white space ".split => ["I", "have", "white", "space"]
若是你只想删除前导和尾随空格(好比PHP的修剪)你可使用.strip
,可是若是要删除全部空格,你可使用.gsub(/\\s+/, "")
代替。 spa
Ruby的.strip
方法执行与trim()
至关的PHP。 code
要删除全部空格: ip
" leading trailing ".squeeze(' ').strip => "leading trailing"
@Tass让我知道个人原始答案连续删除了重复的字母 - 你好! 我已经改用了squish方法,若是使用Rails框架,这种方法更聪明。 get
require 'active_support/all' " leading trailing ".squish => "leading trailing" " good men ".squish => "good men"
引用: http : //apidock.com/rails/String/squish it