Newbie question - how to replace multiple whitespace within astring?

Discussion in 'Ruby' started by Brian Tully, Jun 22, 2004.

  1. Brian Tully

    Brian Tully Guest

    Sorry if this is too basic a question but I just inherited a handful of Ruby
    code and I'm looking to do something that is unbelievably simple to do with
    PHP or Perl, but I'm having no luck figuring out how to do it in Ruby (no
    thanks to "Ruby In A Nutshell").

    In the middle of a Ruby script I have a loop with the variable:

    @user['state']

    Which is populated by data from an external text file. However I'm noticing
    that the text file is not consistent in how it reports the user's State and
    will occasionally add an extra whitespace between States comprised of 2
    words, e.g., "New York".

    In PHP I would simply perform an eregi_replace(" ", " ", $state); and in
    Perl I would do something like $state =~ s/\s\s/\s/$state;

    How would one accomplish this in Ruby?

    Thanks in advance!

    Regards,
    Brian
     
    Brian Tully, Jun 22, 2004
    #1
    1. Advertising

  2. Re: Newbie question - how to replace multiple whitespace within a string?

    Brian Tully wrote:
    > In PHP I would simply perform an eregi_replace(" ", " ", $state); and in
    > Perl I would do something like $state =~ s/\s\s/\s/$state;
    >
    > How would one accomplish this in Ruby?


    "New York".gsub(/ /, ' ')
    => "New York"

    - or better -

    "New \t York".gsub(/\s+/, ' ')
    => "New York"
     
    Andreas Schwarz, Jun 22, 2004
    #2
    1. Advertising

  3. Brian Tully

    Jamey Cribbs Guest

    Re: Newbie question - how to replace multiple whitespace within astring?

    Brian Tully wrote:

    >In PHP I would simply perform an eregi_replace(" ", " ", $state); and in
    >Perl I would do something like $state =~ s/\s\s/\s/$state;
    >
    >How would one accomplish this in Ruby?
    >
    >

    state.squeeze!(" ")

    Now, how easy is that?! :)

    Jamey

    Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.
     
    Jamey Cribbs, Jun 22, 2004
    #3
  4. Brian Tully

    Brian Tully Guest

    Re: Newbie question - how to replace multiple whitespace withinastring?

    on 6/22/04 3:01 PM, Jamey Cribbs at wrote:

    > Brian Tully wrote:
    >
    >> In PHP I would simply perform an eregi_replace(" ", " ", $state); and in
    >> Perl I would do something like $state =~ s/\s\s/\s/$state;
    >>
    >> How would one accomplish this in Ruby?
    >>
    >>

    > state.squeeze!(" ")
    >
    > Now, how easy is that?! :)


    Simply MAH-velous! :) thanks a bunch!

    Just to clarify...

    Should I use state.squeeze(" ") to ensure that it always returns the state
    even if nothing was changed? squeeze! seems to return nil if nothing needs
    to be changed.

    Thanks again!
    Brian
     
    Brian Tully, Jun 22, 2004
    #4
  5. Brian Tully wrote:

    > Should I use state.squeeze(" ") to ensure that it always returns the state
    > even if nothing was changed? squeeze! seems to return nil if nothing needs
    > to be changed.


    str.squeeze! is destructive (changes the String object referred to by
    str), and str.squeeze is not. So it just depends on whether you need to
    keep the original string intact (I'd guess not).
     
    Joel VanderWerf, Jun 22, 2004
    #5
  6. Brian Tully

    Brian Tully Guest

    Re: Newbie question - how to replace multiple whitespace withinastring?

    on 6/22/04 3:36 PM, Joel VanderWerf at wrote:

    > Brian Tully wrote:
    >
    >> Should I use state.squeeze(" ") to ensure that it always returns the state
    >> even if nothing was changed? squeeze! seems to return nil if nothing needs
    >> to be changed.

    >
    > str.squeeze! is destructive (changes the String object referred to by
    > str), and str.squeeze is not. So it just depends on whether you need to
    > keep the original string intact (I'd guess not).



    Hmmm I'm a little hazy...

    I want Ruby to strip out any instances of multiple whitespace and return the
    "correct" string. I also want it to return the string regardless of whether
    the squeeze function modified it.

    So in these examples I would hope for the following:

    @state = @user['state'].squeeze(" ")

    if @user['state'] was originally "New York" I would hope that the above
    would set @state to "New York".

    In addition if @user['state'] was originally "California" I would hope that
    the above would still set @state to "California".



    squeeze! - Squeezes str in place, returning either str, or nil if no
    changes were made.

    I interpret this as if no changes were made "nil" will be returned, so in my
    example of "California" wouldn't this set @state to nil?
     
    Brian Tully, Jun 22, 2004
    #6
  7. Brian Tully

    Jamey Cribbs Guest

    Re: Newbie question - how to replace multiple whitespace withinastring?

    Brian Tully wrote:

    >So in these examples I would hope for the following:
    >
    > @state = @user['state'].squeeze(" ")
    >
    >if @user['state'] was originally "New York" I would hope that the above
    >would set @state to "New York".
    >
    >In addition if @user['state'] was originally "California" I would hope that
    >the above would still set @state to "California".
    >
    >
    >
    >squeeze! - Squeezes str in place, returning either str, or nil if no
    >changes were made.
    >
    >I interpret this as if no changes were made "nil" will be returned, so in my
    >example of "California" wouldn't this set @state to nil?
    >
    >
    >

    You are correct.

    Jamey

    Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.
     
    Jamey Cribbs, Jun 22, 2004
    #7
  8. Brian Tully wrote:

    >on 6/22/04 3:36 PM, Joel VanderWerf at wrote:
    >
    >
    >
    >>Brian Tully wrote:
    >>
    >>
    >>
    >>>Should I use state.squeeze(" ") to ensure that it always returns the state
    >>>even if nothing was changed? squeeze! seems to return nil if nothing needs
    >>>to be changed.
    >>>
    >>>

    >>str.squeeze! is destructive (changes the String object referred to by
    >>str), and str.squeeze is not. So it just depends on whether you need to
    >>keep the original string intact (I'd guess not).
    >>
    >>

    >
    >
    >Hmmm I'm a little hazy...
    >
    >I want Ruby to strip out any instances of multiple whitespace and return the
    >"correct" string. I also want it to return the string regardless of whether
    >the squeeze function modified it.
    >
    >So in these examples I would hope for the following:
    >
    > @state = @user['state'].squeeze(" ")
    >
    >if @user['state'] was originally "New York" I would hope that the above
    >would set @state to "New York".
    >
    >In addition if @user['state'] was originally "California" I would hope that
    >the above would still set @state to "California".
    >
    >
    >
    >

    this is correct

    @user["state"] = "New york"
    @state = @user["state"].squeeze
    p @state #=> "New york"

    @user["state"] = "California"
    @state = @user["state"].squeeze
    p @state #=> "California"

    >squeeze! - Squeezes str in place, returning either str, or nil if no
    >changes were made.
    >
    >I interpret this as if no changes were made "nil" will be returned, so in my
    >example of "California" wouldn't this set @state to nil?
    >
    >
    >

    correct

    you would use squeeze if you wanted to modify @user["state"]. In which
    case the following lines are about equivilent

    @user["state"] = @user["state"].squeeze
    @user["state"].squeeze!

    --
    Mark Sparshatt
     
    Mark Sparshatt, Jun 22, 2004
    #8
  9. Brian Tully

    Brian Tully Guest

    Re: Newbie question - how to replace multiple whitespace withinastring?


    >
    >> squeeze! - Squeezes str in place, returning either str, or nil if no
    >> changes were made.
    >>
    >> I interpret this as if no changes were made "nil" will be returned, so in my
    >> example of "California" wouldn't this set @state to nil?
    >>
    >>
    >>

    > correct
    >
    > you would use squeeze if you wanted to modify @user["state"]. In which
    > case the following lines are about equivilent
    >
    > @user["state"] = @user["state"].squeeze
    > @user["state"].squeeze!
    >


    Aha! Thanks Mark :)

    That clears it up for me :)

    Best regards,
    brian
     
    Brian Tully, Jun 22, 2004
    #9
  10. Brian Tully wrote:
    > on 6/22/04 3:36 PM, Joel VanderWerf at wrote:
    >
    >
    >>Brian Tully wrote:
    >>
    >>
    >>>Should I use state.squeeze(" ") to ensure that it always returns the state
    >>>even if nothing was changed? squeeze! seems to return nil if nothing needs
    >>>to be changed.

    >>
    >>str.squeeze! is destructive (changes the String object referred to by
    >>str), and str.squeeze is not. So it just depends on whether you need to
    >>keep the original string intact (I'd guess not).

    >
    >
    >
    > Hmmm I'm a little hazy...
    >
    > I want Ruby to strip out any instances of multiple whitespace and return the
    > "correct" string. I also want it to return the string regardless of whether
    > the squeeze function modified it.
    >
    > So in these examples I would hope for the following:
    >
    > @state = @user['state'].squeeze(" ")
    >
    > if @user['state'] was originally "New York" I would hope that the above
    > would set @state to "New York".
    >
    > In addition if @user['state'] was originally "California" I would hope that
    > the above would still set @state to "California".


    This is one way:

    @state = @user['state']
    @state.squeeze!(" ")

    After this code, the string referenced by both @state and @user['state']
    will be have no successive pairs of whitespace.
     
    Joel VanderWerf, Jun 25, 2004
    #10
    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. Andrew
    Replies:
    5
    Views:
    7,023
    John C. Bollinger
    Sep 28, 2004
  2. Oli Filth
    Replies:
    9
    Views:
    3,361
    Uncle Pirate
    Jan 17, 2005
  3. François Robert
    Replies:
    3
    Views:
    692
    Toni Uusitalo
    Jul 19, 2005
  4. Replies:
    10
    Views:
    787
    Eric Brunel
    Dec 16, 2008
  5. MRAB
    Replies:
    3
    Views:
    401
Loading...

Share This Page