Forum: Ruby remove all whitespaces in a string

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9fa0e9901902a1eb76217f33bd15c4a2?d=identicon&s=25 jochen kaechelin (Guest)
on 2007-05-28 14:47
(Received via mailing list)
I want to remove all whitespaces in a string.
I know strip to remove whitespaces at the beginning and the end
of the string.

example:

"  bla   @   bla. de      "

should become

"bla@bla.de"

How can I achieve this?

Thanx
D547bd74deebf2145ade3a2ac2286ae0?d=identicon&s=25 Markus Schirp (Guest)
on 2007-05-28 14:51
(Received via mailing list)
"  bla @ bla.de ".gsub!(' ','')
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2007-05-28 14:53
(Received via mailing list)
gsub( ' ', '')

Stefano

Alle lunedì 28 maggio 2007, jochen kaechelin ha scritto:
7e593ac63e5f25649b701dc25f69d1b7?d=identicon&s=25 Luis Parravicini (Guest)
on 2007-05-28 14:55
(Received via mailing list)
On 5/28/07, jochen kaechelin <gissmoh@figgfrosch.de> wrote:
> How can I achieve this?
You can use String.gsub [1]

irb(main):003:0> "  bla   @   bla. de      ".gsub(/\s+/, '')
=> "bla@bla.de"


[1] http://dev.rubycentral.com/ref/ref_c_string.html#gsub
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2007-05-28 14:56
(Received via mailing list)
Sorry, I meant

your_string.gsub(' ', '')

Stefano

Alle lunedì 28 maggio 2007, Stefano Crocco ha scritto:
9c7e3e773ac08cb6e7ae2f4f7eeadaa6?d=identicon&s=25 Servando Garcia (Guest)
on 2007-05-28 15:00
(Received via mailing list)
<html><head><meta name="Generator" content="PSI HTML/CSS Generator"/>
<style type="text/css"><!--
body{font-family:'Tahoma';font-size:10pt;font-color:'#000000';}
LI{display:list-item;margin:0.00in;}
p{display:block;margin:0.00in;}
--></style>
</head><BODY ><div><SPAN style="font-size:10pt;">" bla @ bla. de &#160;
".delete(" ") </SPAN></div>
<div><SPAN style="font-size:10pt;">it maybe a good idea for you to read
the PickAxe<br /><br /></span></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; I want to remove all
whitespaces in a string. I know strip to</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; remove whitespaces at
the beginning and the end of the string.</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; example:</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; " &#160;bla &#160; @
&#160; bla. de &#160; &#160; &#160;"</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; should
become</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;
"bla@bla.de"</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; How can I achieve
this?</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; Thanx</SPAN></div>
</body></html>
9fa0e9901902a1eb76217f33bd15c4a2?d=identicon&s=25 jochen kaechelin (Guest)
on 2007-05-28 15:02
(Received via mailing list)
Markus Schirp schrieb:
> "  bla @ bla.de ".gsub!(' ','')


Mmmh....I already used this syntax.......then there must be a
problem with my RoR code...

I will use the according ML.

Thanx.
9fa0e9901902a1eb76217f33bd15c4a2?d=identicon&s=25 jochen kaechelin (Guest)
on 2007-05-28 15:22
(Received via mailing list)
jochen kaechelin schrieb:
> "bla@bla.de"
>
> How can I achieve this?
>
> Thanx
>
>

It was I RoR mistake i made.
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2007-05-28 15:52
(Received via mailing list)
On 5/28/07, jochen kaechelin <gissmoh@figgfrosch.de> wrote:
> "bla@bla.de"
>
> How can I achieve this?
>
> Thanx
>
>

This is probably slower, but here is another way you can try.

p "  bla   @   bla. de      ".split(/\s+/).join

Harry

--

A Look into Japanese Ruby List in English
http://www.kakueki.com/
Ee469623eb1b8e6e35d192822b9c4aa2?d=identicon&s=25 Florian Aßmann (Guest)
on 2007-05-31 09:14
(Received via mailing list)
Hi,

since Strings are Enumerable I created a singleton method that
overwrites the default one:

def str.each &block
   self.split( // ).each &block
end
str.reject { |char| char =~ /\s/ }

But I don't know if this is going to brake anything...

Sincerely
Florian

Am 28.05.2007 um 15:51 schrieb Harry Kakueki:
This topic is locked and can not be replied to.