Carry over valid_encoding? value in String#replace

Fixes #2899, which in this specific case did not involve `String` subclassing: `String#to_s` calls `String#replace` when `self` is not a `String`, so that explains why it'd show up like that.