Subtracting strings

Is there a method for deleting a string identified in a variable from
another string, e.g.

myunwantedword = "Hello" myphrase = "Hello world" output = myphrase.suchamethod(myunwantedword)

output is now " world"

Regards

John S.

a = “hello world”
b = "hello "
a.sub!(b, “”)

Is what I usually end up using. Hopefully I get corrected with a cleaner
way.

Hello,

yes, there is (two, actually). They are called String#gsub and
String#gsub!, respectively.

Regards

On 20/12/2012 18:22, Calvin Bornhofen wrote:

Hello,

yes, there is (two, actually). They are called String#gsub and
String#gsub!, respectively.

Regards

Many thanks - I did not know one could have a variable as an argument
for sub or sub!

Regards

John S

John S. wrote in post #1089759:

On 20/12/2012 18:22, Calvin Bornhofen wrote:

Hello,

yes, there is (two, actually). They are called String#gsub and
String#gsub!, respectively.

Regards

Many thanks - I did not know one could have a variable as an argument

Any place a literal, e.g. 10, ‘hello’, [1, 2, 3], can be used, a
variable
containing one of those values can be used in its place.

Another way, if you only want to remove the first instance:

myunwantedword = “Hello”
=> “Hello”

myphrase = “Hello world”
=> “Hello world”

output = myphrase.dup
=> “Hello world”

output[myunwantedword] = “”
=> “”

output
=> " world"

Or you can use slice! instead of []=

output.slice!(myunwantedword)
=> “Hello”

output
=> " world"