112

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.
eugen
  • 8,916
  • 11
  • 57
  • 65
Kreeki
  • 3,662
  • 6
  • 27
  • 33

9 Answers9

209

check out Rails squish method:

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

askrynnikov
  • 657
  • 10
  • 15
socjopata
  • 5,028
  • 3
  • 30
  • 33
  • 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 Aug 18 '11 at 12:05
  • 4
    As Kreeki said a tad unclearly, this is a Rails method, not a Ruby method. –  Apr 20 '13 at 07:31
  • 19
    For a non-Rails context, use `some_string.strip.gsub(/\s+/, " ")` which is exactly what squish does. – rapcal Sep 07 '13 at 05:38
  • 1
    Thank you! Didn't know about `squish` – Francois Dec 08 '16 at 16:29
42

To illustrate Rubys built in squeeze:

string.gsub("\n", ' ').squeeze(' ')
steenslag
  • 79,051
  • 16
  • 138
  • 171
  • 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" – charliepark Sep 27 '13 at 20:49
  • 11
    But `squeeze(' ')` just squeezes spaces. `"Squeeze my application's copy".squeeze(' ') => "Squeeze my application's copy"`. – steenslag Sep 27 '13 at 21:16
23

The simplest way would probably be

s = "Hello, my\n       name is Michael."
s.split.join(' ') #=> "Hello, my name is Michael."
Koraktor
  • 41,357
  • 10
  • 69
  • 99
15

Try This:

s = "Hello, my\n       name is Michael."
s.gsub(/\n\s+/, " ")
anusha
  • 2,087
  • 19
  • 31
  • This is actually the best "plain ruby" answer, and works properly for stripping leading spaces from a multiline string. – Koen. Jan 11 '17 at 23:58
7
my_string = "Hello, my\n       name is Michael."
my_string = my_string.gsub( /\s+/, " " )
fl00r
  • 82,987
  • 33
  • 217
  • 237
  • 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 Aug 18 '11 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.

Ali
  • 12,354
  • 9
  • 54
  • 83
4

Use String#gsub:

s = "Hello, my\n       name is Michael."
s.gsub(/\s+/, " ")
Nikola
  • 694
  • 8
  • 15
  • 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 Aug 18 '11 at 11:58
3
Use squish
currency = " XCD"
str = currency.squish
 str = "XCD" #=> "XCD"
vikas pal
  • 75
  • 1
  • 9
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.

Convincible
  • 314
  • 2
  • 10