How do I remove repeated spaces in a string?

前端 未结 7 1730
悲哀的现实
悲哀的现实 2020-12-29 20:05

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

相关标签:
7条回答
  • 2020-12-29 20:50

    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"
    
    0 讨论(0)
提交回复
热议问题