code: public static String tr(String input, String from, String to) {
      assert from.length() == to.length();
      StringBuilder sb = null;
      int len = input.length();
      for (int i = 0; i < len; i++) {
        int ind = from.indexOf(input.charAt(i));
        if (ind >= 0) {
          if (sb == null) {
            sb = new StringBuilder(input);
          }
          sb.setCharAt(i, to.charAt(ind));
        }
      }
      if (sb == null) {
        return input;
      } else {
        return sb.toString();
      }
    }
docs: Swap any occurrences of any characters in the from String in the input String with
  the corresponding character from the to String.  As Perl tr, for example,
  tr("chris", "irs", "mop").equals("chomp"), except it does not
  support regular expression character ranges.
  <p>
  <i>Note:</i> This is now optimized to not allocate any objects if the
  input is returned unchanged.