How do I remove the first occurence of a substring in string?
sub
will do what you want. gsub
is the global version that you're probably already familiar with.
Use sub!
to substitute what you are trying to find with ""(nothing), thereby deleting it:
phrase.sub!("foo", "")
The !(bang) at the end makes it permanent. sub
is different then gsub
in that sub
just substitutes the first instance of the string that you are trying to find whereas gsub
finds all instances.