How to strip inner whitespace in rails

I want to strip any whitespace that more than 1 to be 1

For example "A B C" ===> "A B C"

            "Very Important" ===> "Very Important"

···

--
Posted via http://www.ruby-forum.com/.

try #squeeze

···

On Mon, Jul 20, 2009 at 10:47 AM, Thriving K.<superdisconnect@hotmail.com> wrote:

I want to strip any whitespace that more than 1 to be 1

For example "A B C" ===> "A B C"

       &quot;Very    Important&quot; ===&gt; &quot;Very Important&quot;

Thriving K. wrote:

I want to strip any whitespace that more than 1 to be 1

For example "A B C" ===> "A B C"

            "Very Important" ===> "Very Important"

split() rules the world. squeez()...not so much.

s.split().join(" ")

···

--
Posted via http://www.ruby-forum.com/\.

Thriving K. wrote:

I want to strip any whitespace that more than 1 to be 1

For example "A B C" ===> "A B C"

            "Very Important" ===> "Very Important"

In Rails (and nobody mentioned it), use String#squish or String#squish!

···

--
Posted via http://www.ruby-forum.com/\.

botp wrote:

···

On Mon, Jul 20, 2009 at 10:47 AM, Thriving > K.<superdisconnect@hotmail.com> wrote:

I want to strip any whitespace that more than 1 to be 1

For example "A � �B � �C" ===> "A B C"

� � � � � �"Very � �Important" ===> "Very Important"

try #squeeze

Thank you
--
Posted via http://www.ruby-forum.com/\.

7stud -- wrote:

split() rules the world. squeeze()...not so much.

...but if you are a regex addict, then gsub() is your fix:

result = s.gsub(/\s+/) do |match|
  " "
end

···

--
Posted via http://www.ruby-forum.com/\.

Why? String#squeeze performs precisely what the OP asks for. There's
nothing as concise as that.

If what needs to be collapsed is whitespace in the \s sense, then I
would perhaps swtich to gsub:

   str.gsub(/\s+/, ' ')

···

On Mon, Jul 20, 2009 at 5:15 AM, 7stud --<bbxx789_05ss@yahoo.com> wrote:

split() rules the world. squeez()...not so much.

Hi --

Thriving K. wrote:

I want to strip any whitespace that more than 1 to be 1

For example "A B C" ===> "A B C"

            "Very Important" ===> "Very Important"

split() rules the world. squeez()...not so much.

s.split().join(" ")

They're completely different methods, though. They're not in a death
struggle :slight_smile: They do different things. I wouldn't want to try this
with split:

   "abc deffff ghi".squeeze(" f") => "abc def ghi"

squeeze is a better fit, it terms of its stated purpose, than split,
if you want to condense multiple occurrences into one. It's also
faster, in every benchmark I can come up with:

        user system total real
    2.630000 0.010000 2.640000 ( 2.665775)
    0.140000 0.000000 0.140000 ( 0.136925)

(That's for removing extra " "s from a string of length about 500.)

David

···

On Mon, 20 Jul 2009, 7stud -- wrote:

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2\)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN\)

Hi --

···

On Tue, 21 Jul 2009, Andrew Kaspick wrote:

Thriving K. wrote:

I want to strip any whitespace that more than 1 to be 1

For example "A B C" ===> "A B C"

            "Very Important" ===> "Very Important"

In Rails (and nobody mentioned it), use String#squish or String#squish!

The question was about removing inner whitespace, though. The only
thing squish does that squeeze or gsub don't do is remove outer
whitespace.

David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2\)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN\)

Hi --

···

On Mon, 20 Jul 2009, Xavier Noria wrote:

On Mon, Jul 20, 2009 at 5:15 AM, 7stud --<bbxx789_05ss@yahoo.com> wrote:

split() rules the world. squeez()...not so much.

Why? String#squeeze performs precisely what the OP asks for. There's
nothing as concise as that.

If what needs to be collapsed is whitespace in the \s sense, then I
would perhaps swtich to gsub:

  str.gsub(/\s+/, ' ')

I agree, unless speed is an issue, in which case you might be better
off with:

   str.squeeze(" \t\n\r\f")

although it's a bit more heavy-handed and I'm probably forgetting one.

David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2\)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN\)

Agreed, I would default to gsub here because I find \s more obvious
than the character set, but if speed is an issue I'd go with sqeeze as
well.

···

On Mon, Jul 20, 2009 at 1:23 PM, David A. Black<dblack@rubypal.com> wrote:

I agree, unless speed is an issue, in which case you might be better
off with:

str.squeeze(" \t\n\r\f")

although it's a bit more heavy-handed and I'm probably forgetting one.