Use gsub intead of tr when the to_str is longer than the from_str

ref https://github.com/rails/rails/pull/9564#issuecomment-14464827