How do I remove the first occurence of a substring in string?
ruby, ruby-on-rails, string, substring
Solution
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.
Problem
How can I remove the first occurence of a given substring? I have: ``` phrase = "foobarfoo" ``` and, when I call: ``` phrase.some_method_i_dont_know_yet ("foo") ``` I want the phrase to look like `barfoo`. I tried with `delete` and `slice` but the first removes all the occurrences but the second just returns the slice.