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"
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"
On Mon, Jul 20, 2009 at 10:47 AM, Thriving
K.[email protected] 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
botp wrote:
On Mon, Jul 20, 2009 at 10:47 AM, Thriving
K.[email protected] 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
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(" ")
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
On Mon, Jul 20, 2009 at 5:15 AM, 7stud –[email protected] 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+/, ’ ')
Hi –
On Mon, 20 Jul 2009, 7stud – 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"
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
Hi –
On Mon, 20 Jul 2009, Xavier N. wrote:
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
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!
On Mon, Jul 20, 2009 at 1:23 PM, David A. Black[email protected]
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.
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.
Hi –
On Tue, 21 Jul 2009, Andrew K. 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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs