Remove all whitespaces in a string


#1

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


#2

" bla @ bla.de ".gsub!(’ ‘,’’)


#3

gsub( ’ ', ‘’)

Stefano

Alle lunedì 28 maggio 2007, jochen kaechelin ha scritto:


#4

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


#5

Sorry, I meant

your_string.gsub(’ ', ‘’)

Stefano

Alle lunedì 28 maggio 2007, Stefano C. ha scritto:


#6
" bla @ bla. de   ".delete(" ")
it maybe a good idea for you to read the PickAxe

> 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

#7

jochen kaechelin schrieb:

“removed_email_address@domain.invalid”

How can I achieve this?

Thanx

It was I RoR mistake i made.


#8

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.


#9

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


#10

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/