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

2008/9/1 Zhao Yi [email protected]:

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

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

Please look it up in the docs

http://www.ruby-doc.org/

Cheers

robert

On Mon, Sep 1, 2008 at 10:36 AM, Zhao Yi [email protected] 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] http://apidock.com/ruby/String/gsub
[2] http://apidock.com/ruby/String/gsub

Thomas W. wrote:

On Mon, Sep 1, 2008 at 10:36 AM, Zhao Yi [email protected] 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] http://apidock.com/ruby/String/gsub
[2] http://apidock.com/ruby/String/gsub

ok thanks.

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