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.
jochen kaechelin (Guest)
on 2007-05-28 16: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

"removed_email_address@domain.invalid"

How can I achieve this?

Thanx
Markus S. (Guest)
on 2007-05-28 16:51
(Received via mailing list)
"  bla @ bla.de ".gsub!(' ','')
Stefano C. (Guest)
on 2007-05-28 16:53
(Received via mailing list)
gsub( ' ', '')

Stefano

Alle lunedì 28 maggio 2007, jochen kaechelin ha scritto:
Luis P. (Guest)
on 2007-05-28 16:55
(Received via mailing list)
On 5/28/07, jochen kaechelin <removed_email_address@domain.invalid> wrote:
> How can I achieve this?
You can use String.gsub [1]

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


[1] http://dev.rubycentral.com/ref/ref_c_string.html#gsub
Stefano C. (Guest)
on 2007-05-28 16:56
(Received via mailing list)
Sorry, I meant

your_string.gsub(' ', '')

Stefano

Alle lunedì 28 maggio 2007, Stefano C. ha scritto:
Servando G. (Guest)
on 2007-05-28 17: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;
"removed_email_address@domain.invalid"</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>
jochen kaechelin (Guest)
on 2007-05-28 17:02
(Received via mailing list)
Markus S. 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.
jochen kaechelin (Guest)
on 2007-05-28 17:22
(Received via mailing list)
jochen kaechelin schrieb:
> "removed_email_address@domain.invalid"
>
> How can I achieve this?
>
> Thanx
>
>

It was I RoR mistake i made.
Harry K. (Guest)
on 2007-05-28 17:52
(Received via mailing list)
On 5/28/07, jochen kaechelin <removed_email_address@domain.invalid> wrote:
> "removed_email_address@domain.invalid"
>
> 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/
Florian Aßmann (Guest)
on 2007-05-31 11: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 K.:
This topic is locked and can not be replied to.