remove all whitespaces in a string

Discussion in 'Ruby' started by jochen kaechelin, May 28, 2007.

  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

    ""

    How can I achieve this?

    Thanx
     
    jochen kaechelin, May 28, 2007
    #1
    1. Advertising

  2. " bla @ bla.de ".gsub!(' ','')


    On Monday 28 May 2007 14:46:26 jochen kaechelin wrote:
    > 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
    >
    > ""
    >
    > How can I achieve this?
    >
    > Thanx
     
    Markus Schirp, May 28, 2007
    #2
    1. Advertising

  3. gsub( ' ', '')

    Stefano

    Alle luned=C3=AC 28 maggio 2007, jochen kaechelin ha scritto:
    > 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
    >
    > ""
    >
    > How can I achieve this?
    >
    > Thanx
     
    Stefano Crocco, May 28, 2007
    #3
  4. On 5/28/07, jochen kaechelin <> wrote:
    > 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
    > ""
    >
    > How can I achieve this?


    You can use String.gsub [1]

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


    [1] http://dev.rubycentral.com/ref/ref_c_string.html#gsub

    --
    Luis Parravicini
    http://ktulu.com.ar/blog/
     
    Luis Parravicini, May 28, 2007
    #4
  5. Sorry, I meant

    your_string.gsub(' ', '')

    Stefano

    Alle luned=C3=AC 28 maggio 2007, Stefano Crocco ha scritto:
    > gsub( ' ', '')
    >
    > Stefano
    >
    > Alle luned=C3=AC 28 maggio 2007, jochen kaechelin ha scritto:
    > > 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
    > >
    > > ""
    > >
    > > How can I achieve this?
    > >
    > > Thanx
     
    Stefano Crocco, May 28, 2007
    #5
  6. 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.
     
    jochen kaechelin, May 28, 2007
    #6
  7. Re: SOLVED remove all whitespaces in a string

    jochen kaechelin schrieb:
    > 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
    >
    > ""
    >
    > How can I achieve this?
    >
    > Thanx
    >
    >


    It was I RoR mistake i made.
     
    jochen kaechelin, May 28, 2007
    #7
  8. On 5/28/07, jochen kaechelin <> wrote:
    > 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
    >
    > ""
    >
    > 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/
     
    Harry Kakueki, May 28, 2007
    #8
  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 Kakueki:

    > On 5/28/07, jochen kaechelin <> wrote:
    >> 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
    >>
    >> ""
    >>
    >> 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, May 31, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joona I Palaste
    Replies:
    1
    Views:
    503
    Berco
    Jan 7, 2004
  2. Berco
    Replies:
    0
    Views:
    1,718
    Berco
    Jan 7, 2004
  3. Franz Müller

    Removing all Whitespaces with preprocessor

    Franz Müller, Jul 9, 2009, in forum: C Programming
    Replies:
    7
    Views:
    347
    luserXtrog
    Jul 9, 2009
  4. Stefan Behnel
    Replies:
    1
    Views:
    1,026
    David Vicente
    Feb 9, 2011
  5. Vellingiri Arul
    Replies:
    3
    Views:
    114
    Xavier Noria
    Sep 19, 2007
Loading...

Share This Page