I have a string:
\"foo (2 spaces) bar (3 spaces) baaar (6 spaces) fooo\"
How do I remove repetitious spaces in it so there shou
Important note: this is answer for Ruby on Rails, not plain ruby
(both Activesupport
and Facets
are part of Rails
gem)
To complement the other answers, note that both Activesupport and Facets provide String#squish ([update] caveat: it also removes newlines within the string):
>> "foo bar bar baaar".squish
=> "foo bar bar baaar"