Replacing with "" / null is missing. I first looked at the function and modified it. Quickly noticed that a few unit tests were missing: assert(replace("foo", "foo", "") == ""); assert(replace("foo", "foo", null) == "");