How to strip inner whitespace in rails

T

Thriving K.

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"
 
B

botp

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

For example "A =A0 =A0B =A0 =A0C" =3D=3D=3D> "A B C"

=A0 =A0 =A0 =A0 =A0 =A0"Very =A0 =A0Important" =3D=3D=3D> "Very Important=
"

try #squeeze
 
7

7stud --

Thriving said:
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(" ")
 
7

7stud --

7stud said:
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
 
X

Xavier Noria

split() rules the world. =C2=A0squeez()...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+/, ' ')
 
D

David A. Black

Hi --

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

s.split().join(" ")

They're completely different methods, though. They're not in a death
struggle :) 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

--
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)
 
X

Xavier Noria

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

=C2=A0str.squeeze(" \t\n\r\f")

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

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.
 
A

Andrew Kaspick

Thriving said:
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!
 
D

David A. Black

Hi --

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top