Remove multiple spaces and new lines inside of String

Solution 1:

check out Rails squish method:

http://apidock.com/rails/String/squish

Solution 2:

To illustrate Rubys built in squeeze:

string.gsub("\n", ' ').squeeze(' ')

Solution 3:

The simplest way would probably be

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

Solution 4:

Try This:

s = "Hello, my\n       name is Michael."
s.gsub(/\n\s+/, " ")

Solution 5:

my_string = "Hello, my\n       name is Michael."
my_string = my_string.gsub( /\s+/, " " )