How to replace a string with given pattern?

I want to replace a string with the new value on a given regular
pattern. For example:

"Hello".replace("/H/", "h") #=> hello

···

--
Posted via http://www.ruby-forum.com/.

Please look it up in the docs

Cheers

robert

···

2008/9/1 Zhao Yi <youhaodeyi@gmail.com>:

I want to replace a string with the new value on a given regular
pattern. For example:

"Hello".replace("/H/", "h") #=> hello

--
use.inject do |as, often| as.you_can - without end

You can use String#sub[1] to replace one occurance:
"HHello".sub(/H/, "h") #=> "hHello"

or gsub[2] to replace all:
"HHello".gsub(/H/, "h")

[1] gsub (String) - APIdock
[2] gsub (String) - APIdock

···

On Mon, Sep 1, 2008 at 10:36 AM, Zhao Yi <youhaodeyi@gmail.com> wrote:

I want to replace a string with the new value on a given regular
pattern. For example:

"Hello".replace("/H/", "h") #=> hello

Zhao Yi wrote:

I want to replace a string with the new value on a given regular
pattern. For example:

"Hello".replace("/H/", "h") #=> hello

String#gsub (which is in the docs, btw)

HTH,
Sebastian

···

--
NP: Amon Amarth - Twilight of the Thunder God
Jabber: sepp2k@jabber.org
ICQ: 205544826

Thomas Wieczorek wrote:

···

On Mon, Sep 1, 2008 at 10:36 AM, Zhao Yi <youhaodeyi@gmail.com> wrote:

I want to replace a string with the new value on a given regular
pattern. For example:

"Hello".replace("/H/", "h") #=> hello

You can use String#sub[1] to replace one occurance:
"HHello".sub(/H/, "h") #=> "hHello"

or gsub[2] to replace all:
"HHello".gsub(/H/, "h")

[1] gsub (String) - APIdock
[2] gsub (String) - APIdock

ok thanks.
--
Posted via http://www.ruby-forum.com/\.