Remove substring from the string Remove substring from the string ruby ruby

Remove substring from the string


You can use the slice method:

a = "foobar"a.slice! "foo"=> "foo"a=> "bar"

there is a non '!' version as well. More info can be seen in the documentation about other versions as well:http://www.ruby-doc.org/core/classes/String.html#method-i-slice-21


How about str.gsub("subString", "") Check out the Ruby Doc


If it is a the end of the string, you can also use chomp:

"hello".chomp("llo")     #=> "he"