114

Suppose we have string like this:

Hello, my\n       name is Michael.

How can I remove that new line and strip those spaces after that into one inside of string to get this?

Hello, my name is Michael.

9 Answers 9

213

check out Rails squish method:

https://api.rubyonrails.org/classes/String.html#method-i-squish

3
  • 7
    The simplest and the most elegant solution. Thanks. But there's one thing, this method is defined in Rails, so it'll work only in Rails applications, luckily that's my case.
    – Kreeki
    Commented Aug 18, 2011 at 12:05
  • 4
    As Kreeki said a tad unclearly, this is a Rails method, not a Ruby method.
    – user636044
    Commented Apr 20, 2013 at 7:31
  • 21
    For a non-Rails context, use some_string.strip.gsub(/\s+/, " ") which is exactly what squish does.
    – rapcal
    Commented Sep 7, 2013 at 5:38
42

To illustrate Rubys built in squeeze:

string.gsub("\n", ' ').squeeze(' ')
2
  • 4
    Just be aware: squeeze will compress ALL strings that come in runs of multiple characters. So "Squeeze my application's copy".squeeze => "Squeze my aplication's copy" Commented Sep 27, 2013 at 20:49
  • 11
    But squeeze(' ') just squeezes spaces. "Squeeze my application's copy".squeeze(' ') => "Squeeze my application's copy".
    – steenslag
    Commented Sep 27, 2013 at 21:16
22

The simplest way would probably be

s = "Hello, my\n       name is Michael."
s.split.join(' ') #=> "Hello, my name is Michael."
0
15

Try This:

s = "Hello, my\n       name is Michael."
s.gsub(/\n\s+/, " ")
1
  • This is actually the best "plain ruby" answer, and works properly for stripping leading spaces from a multiline string.
    – Koen.
    Commented Jan 11, 2017 at 23:58
7
my_string = "Hello, my\n       name is Michael."
my_string = my_string.gsub( /\s+/, " " )
1
  • he would also like to remove the \n ideally (I feel) it should be replace with a space in case of something like "Hello,\nMy name is Michael"
    – Ali
    Commented Aug 18, 2011 at 12:01
5

this regex will replace instance of 1 or more white spaces with 1 white space, p.s \s will replace all white space characters which includes \s\t\r\n\f:

a_string.gsub!(/\s+/, ' ')

Similarly for only carriage return

str.gsub!(/\n/, " ")

First replace all \n with white space, then use the remove multiple white space regex.

1
  • Thanks! This worked for my purposes to remove multiples spaces but maintain single space between words. Commented Dec 8, 2012 at 21:03
4

Use String#gsub:

s = "Hello, my\n       name is Michael."
s.gsub(/\s+/, " ")
1
  • output would not be correct in the case of "Hello,\nMy name is Michael". Ideally you would want a space where there is a \n
    – Ali
    Commented Aug 18, 2011 at 11:58
3
Use squish
currency = " XCD"
str = currency.squish
 str = "XCD" #=> "XCD"
2

You can add just the squish method (and nothing else) to Ruby by including just this Ruby Facet:

https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/squish.rb

require 'facets/string/squish'

Then use

"my    \n   string".squish #=> "my string"

Doesn't require Rails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.