code: public static String substitute(EvaluationContext ctx, Object text, Object oldText, Object newText, @IntegerDefault(-1) Object instanceNum) {
          String _text = Conversions.toString(text, ctx);
          String _oldText = Conversions.toString(oldText, ctx);
          String _newText = Conversions.toString(newText, ctx);
          int _instanceNum = Conversions.toInteger(instanceNum, ctx);
  
          if (_instanceNum < 0) {
              return _text.replace(_oldText, _newText);
          }
          else {
              String[] splits = _text.split(_oldText);
              StringBuilder output = new StringBuilder(splits[0]);
              for (int s = 1; s < splits.length; s++) {
                  String sep = s == _instanceNum ? _newText : _oldText;
                  output.append(sep);
                  output.append(splits[s]);
              }
              return output.toString();
          }
      }
docs: Substitutes new_text for old_text in a text string