string#sub all instances of pattern???

Discussion in 'Ruby' started by Nick Bo, Sep 29, 2008.

  1. Nick Bo

    Nick Bo Guest

    eg: str = "a b c d"
    newStr = str.sub(" ", ", ")

    gives me output a, b c d

    what i want is a, b, c, d

    ow do i make this happen sub isnt doing it for me i guess. I been
    looking all through the ruby-doc to no avail.
    --
    Posted via http://www.ruby-forum.com/.
    Nick Bo, Sep 29, 2008
    #1
    1. Advertising

  2. Hi --

    On Mon, 29 Sep 2008, Nick Bo wrote:

    > eg: str = "a b c d"
    > newStr = str.sub(" ", ", ")
    >
    > gives me output a, b c d
    >
    > what i want is a, b, c, d
    >
    > ow do i make this happen sub isnt doing it for me i guess. I been
    > looking all through the ruby-doc to no avail.


    Check out String#gsub, and also Array#join.


    David

    --
    Rails training from David A. Black and Ruby Power and Light:
    Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
    Advancing with Rails January 19-22 Fort Lauderdale, FL *
    * Co-taught with Patrick Ewing!
    See http://www.rubypal.com for details and updates!
    David A. Black, Sep 29, 2008
    #2
    1. Advertising

  3. Nick Bo

    Jamey Cribbs Guest

    try gsub instead of sub.

    Jamey


    On Sun, Sep 28, 2008 at 9:04 PM, Nick Bo <> wrote:
    > eg: str = "a b c d"
    > newStr = str.sub(" ", ", ")
    >
    > gives me output a, b c d
    >
    > what i want is a, b, c, d
    >
    > ow do i make this happen sub isnt doing it for me i guess. I been
    > looking all through the ruby-doc to no avail.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    >
    Jamey Cribbs, Sep 29, 2008
    #3
  4. Nick Bo

    Nick Bo Guest

    Nick Bo, Sep 29, 2008
    #4
  5. Nick Bo

    Mike Stok Guest

    On Sep 28, 2008, at 9:04 PM, Nick Bo wrote:

    > eg: str = "a b c d"
    > newStr = str.sub(" ", ", ")
    >
    > gives me output a, b c d
    >
    > what i want is a, b, c, d
    >
    > ow do i make this happen sub isnt doing it for me i guess. I been
    > looking all through the ruby-doc to no avail.


    You want to use str.gsub rather than str.sub. Compare the
    documentation of the two:

    ------------------------------------------------------------ String#gsub
    str.gsub(pattern, replacement) => new_str
    str.gsub(pattern) {|match| block } => new_str

    From Ruby 1.9.0
    ------------------------------------------------------------------------
    Returns a copy of _str_ with _all_ occurrences of _pattern_
    replaced with either _replacement_ or the value of the block. The
    _pattern_ will typically be a +Regexp+; if it is a +String+ then
    no
    regular expression metacharacters will be interpreted (that is
    +/\d/+ will match a digit, but +'\d'+ will match a backslash
    followed by a 'd').

    [...]

    ------------------------------------------------------------- String#sub
    str.sub(pattern, replacement) => new_str
    str.sub(pattern) {|match| block } => new_str

    From Ruby 1.9.0
    ------------------------------------------------------------------------
    Returns a copy of _str_ with the _first_ occurrence of _pattern_
    replaced with either _replacement_ or the value of the block. The
    _pattern_ will typically be a +Regexp+; if it is a +String+ then
    no
    regular expression metacharacters will be interpreted (that is
    +/\d/+ will match a digit, but +'\d'+ will match a backslash
    followed by a 'd').

    [...]

    Hope this helps,

    Mike


    --

    Mike Stok <>
    http://www.stok.ca/~mike/

    The "`Stok' disclaimers" apply.
    Mike Stok, Sep 29, 2008
    #5
  6. Nick Bo

    Tim Hunter Guest

    Nick Bo wrote:
    > eg: str = "a b c d"
    > newStr = str.sub(" ", ", ")
    >
    > gives me output a, b c d
    >
    > what i want is a, b, c, d
    >
    > ow do i make this happen sub isnt doing it for me i guess. I been
    > looking all through the ruby-doc to no avail.


    gsub

    --
    RMagick: http://rmagick.rubyforge.org/
    Tim Hunter, Sep 29, 2008
    #6
    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 Wohlbier
    Replies:
    2
    Views:
    354
    Josiah Carlson
    Feb 22, 2004
  2. Ben
    Replies:
    2
    Views:
    855
  3. Replies:
    8
    Views:
    443
    James Stroud
    Jan 29, 2009
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    1,947
    Lawrence D'Oliveiro
    May 20, 2011
  5. laredotornado
    Replies:
    3
    Views:
    96
Loading...

Share This Page