spaces in strings

Discussion in 'Ruby' started by Joonas Lindholm, Aug 26, 2009.

  1. I have a string like '1 334.64' and I need to remove the space so that
    it becomes '1334.64'. I have I think ruby 1.6 in home computer where
    worked gsub!(/\s/, '') but in other computer I have ruby 1.7 and it
    doesn't seem to work.
    --
    Posted via http://www.ruby-forum.com/.
     
    Joonas Lindholm, Aug 26, 2009
    #1
    1. Advertising

  2. Joonas Lindholm

    7stud -- Guest

    Joonas Lindholm wrote:
    > Brian Candler wrote:
    >
    >> ruby 1.7 was only a development version, so you should move to 1.8.6 if
    >> possible.
    >>
    >> However, "it doesn't seem to work" is not a useful problem description.
    >> You need to show exactly what you ran, the exact output (copy-paste),
    >> what you expected the output to be, and what platform you are running
    >> under.
    >>
    >> The code snippet you have shown looks OK to me.

    >
    > Ups, what I meant is that the 1.8.6 is the version I have home and at
    > other computer 1.8.7. But if the gsub!(/\s/, '') looks fine I have to do
    > more testing..


    I get identical results with 1.8.6 and ruby ruby 1.9.1L

    str = "1 334.64"
    puts str

    str.gsub!(/\s/, "")
    puts str

    --output:--
    1 334.64
    1334.64

    I have a hard time believing that ruby 1.8.7 would produce different
    results--no matter how badly it is screwed up.
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Aug 26, 2009
    #2
    1. Advertising

  3. Joonas Lindholm

    Ralf Mueller Guest

    Joonas Lindholm wrote:
    > Brian Candler wrote:
    >
    >
    >> ruby 1.7 was only a development version, so you should move to 1.8.6 if
    >> possible.
    >>
    >> However, "it doesn't seem to work" is not a useful problem description.
    >> You need to show exactly what you ran, the exact output (copy-paste),
    >> what you expected the output to be, and what platform you are running
    >> under.
    >>
    >> The code snippet you have shown looks OK to me.
    >>

    >
    > Ups, what I meant is that the 1.8.6 is the version I have home and at
    > other computer 1.8.7. But if the gsub!(/\s/, '') looks fine I have to do
    > more testing..
    >

    String#tr works similar with easier interface:
    http://www.ruby-doc.org/core/classes/String.html#M000830
     
    Ralf Mueller, Aug 26, 2009
    #3
  4. 2009/8/26 Glenn Jackman <>:
    > At 2009-08-26 12:37PM, "Joonas Lindholm" wrote:
    >> =A0I have a string like '1 334.64' and =A0I need to remove the space so =

    that
    >> =A0it becomes '1334.64'. I have I think ruby 1.6 in home computer where
    >> =A0worked gsub!(/\s/, '') but in other computer I have ruby 1.7 and it
    >> =A0doesn't seem to work.

    >
    > An option nobody has suggested so far: =A0String#delete
    >
    > =A0 =A0"1 334.64".delete(" ") =A0# =3D=3D> "1334.64"


    Another option that I haven't seen suggested:

    str.gsub!(/\s+/, '')

    Could be a bit faster than /\s/ if there are longer sequences of spaces.

    Cheers

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Aug 27, 2009
    #4
    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. John B. Matthews
    Replies:
    4
    Views:
    671
    John B. Matthews
    Sep 12, 2008
  2. Roedy Green
    Replies:
    3
    Views:
    627
  3. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,610
    Jan C. Faerber
    Sep 10, 2009
  4. Tomasz Chmielewski

    how can I show spaces as spaces, part 2

    Tomasz Chmielewski, Sep 9, 2009, in forum: HTML
    Replies:
    14
    Views:
    878
    Jan C. Faerber
    Sep 10, 2009
  5. johkar
    Replies:
    2
    Views:
    2,903
    Mayeul
    Dec 10, 2009
Loading...

Share This Page