[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

string#sub all instances of pattern???

Nick Bo

9/29/2008 1:05:00 AM

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

5 Answers

David A. Black

9/29/2008 1:10:00 AM

0

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.r... for details and updates!

Jamey Cribbs

9/29/2008 1:12:00 AM

0

try gsub instead of sub.

Jamey


On Sun, Sep 28, 2008 at 9:04 PM, Nick Bo <bornemann1@nku.edu> 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-....
>
>
>

Nick Bo

9/29/2008 1:14:00 AM

0

perfect thanks
--
Posted via http://www.ruby-....

Mike Stok

9/29/2008 1:14:00 AM

0


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 <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.





Tim Hunter

9/29/2008 1:16:00 AM

0

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