删除所有空格的Ruby函数是什么? 有点像php的trim()
?
#1楼
s = "I have white space".delete(' ')
并模拟PHP的trim()
函数:
s = " I have leading and trailing white space ".strip
#2楼
相关回答:
" clean up my edges ".strip
回报
"clean up my edges"
#3楼
也别忘了:
$ s = " I have white space ".split
=> ["I", "have", "white", "space"]
#4楼
如果你只想删除前导和尾随空格(比如PHP的修剪)你可以使用.strip
,但是如果要删除所有空格,你可以使用.gsub(/\\s+/, "")
代替。
#5楼
Ruby的.strip
方法执行与trim()
相当的PHP。
要删除所有空格:
" leading trailing ".squeeze(' ').strip
=> "leading trailing"
@Tass让我知道我的原始答案连续删除了重复的字母 - 你好! 我已经改用了squish方法,如果使用Rails框架,这种方法更聪明。
require 'active_support/all'
" leading trailing ".squish
=> "leading trailing"
" good men ".squish
=> "good men"
引用: http : //apidock.com/rails/String/squish
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/3156340