we use this method to replace apache StringEscapeUtils, which is slower
557177
private static String _escapeXML(String value) {
        int len = value.length();
        if (0 == len) {
            return value;
        }
        StringBuilder buf = null;
        for (int i = 0; i < len; i++) {
            char ch = value.charAt(i);
            switch (ch) {
                case '<':
                    if (buf == null) {
                        buf = new StringBuilder(len * 2);
                        if (i > 0) {
                            buf.append(value.substring(0, i));
                        }
                    }
                    buf.append("&lt;");
                    break;
                case '>':
                    if (buf == null) {
                        buf = new StringBuilder(len * 2);
                        if (i > 0) {
                            buf.append(value.substring(0, i));
                        }
                    }
                    buf.append("&gt;");
                    break;
                case '\"':
                    if (buf == null) {
                        buf = new StringBuilder(len * 2);
                        if (i > 0) {
                            buf.append(value.substring(0, i));
                        }
                    }
                    buf.append("&quot;");
                    break;
                case '\'':
                    if (buf == null) {
                        buf = new StringBuilder(len * 2);
                        if (i > 0) {
                            buf.append(value.substring(0, i));
                        }
                    }
                    buf.append("&apos;");
                    break;
                case '&':
                    if (buf == null) {
                        buf = new StringBuilder(len * 2);
                        if (i > 0) {
                            buf.append(value.substring(0, i));
                        }
                    }
                    buf.append("&amp;");
                    break;
                default:
                    if (buf != null) {
                        buf.append(ch);
                    }
                    break;
            }
        }
        if (buf != null) {
            return buf.toString();
        }
        return value;
    }
360541
public void replace(String what, Object with) {
    if (what.charAt(0)=='$')
      throw new RuntimeException("$ is now control char that denotes URL encoding!");
    for (Placeholder p : _placeholders.get(what))
      p.end.insertAndAdvance(with.toString());

    for (Placeholder p : _placeholders.get("$"+what))
      try {
        p.end.insertAndAdvance(URLEncoder.encode(with.toString(),"UTF-8"));
      } catch (IOException e) {
        p.end.insertAndAdvance(e.toString());
      }
  }
665925
function(src, stringToFind, stringToReplace) {

      var temp, index;

      temp = src;
      index = temp.indexOf(stringToFind);

      while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
      }

      return temp;
    }
31296
public void replace (String... replace)
    {
        HashSet<String> toAdd = Sets.newHashSet();
        Iterator<String> i = _imports.iterator();
        while (i.hasNext()) {
            String name = i.next();
            for (int j = 0; j < replace.length; j += 2) {
                if (name.equals(replace[j])) {
                    toAdd.add(replace[j + 1]);
                    i.remove();
                    break;
                }
            }
        }
        _imports.addAll(toAdd);
    }
580488
def replace(self, key, val, time=0, min_compress_len=0):
        '''
        '''
        return self._set("replace", key, val, time, min_compress_len)
991409
function replaceInString(string, variables) {
    const model = createModel(string);
    let offset = 0;
    let output = '';

    for (let i = 0, il = model.variables.length; i < il; i++) {
        const v = model.variables[i];
        let value = v.name in variables ? variables[v.name] : v.name;
        if (typeof value === 'function') {
            value = value(model.string, v, offset + v.location);
        }

        output += model.string.slice(offset, v.location) + value;
        offset = v.location + v.length;
    }

    return output + model.string.slice(offset);
}
1291566
function replaceKeys(searchString, replacement) {
  const replacementKeys = {
    COMPONENT_NAME: replacement,
    component_name: replacement.toLowerCase(),
    COMPONENT_CAP_NAME: replacement.toUpperCase(),
    cOMPONENT_NAME: replacement[0].toLowerCase() + replacement.substr(1),
  }

  return Object.keys(replacementKeys).reduce(
    (acc, curr) => {
      if (acc.includes(curr)) {
        const regEx = new RegExp(curr, 'g')
        return acc.replace(regEx, replacementKeys[curr])
      }
      return acc
    },
    searchString
  )
}
1018333
private String replaceSpecialsWithSub(String inString) 
  {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
      tc.entry(cclass,
               "replaceSpecialsWithSub",
               "inString: " + inString);    
    
    String outString = null;
    StringBuffer sb = null;
    
    // First of all look for ampersands
    int posAmpersand = inString.indexOf("@");
    if(posAmpersand >=0)
    {
      // Need to do some work, the predicate has an attribute char
      sb = new StringBuffer(inString);
    }

    // Locate and replace the ampersands
    while(posAmpersand >=0)
    {
      // Replace the ampersand with "_$AMP$_"
      sb.replace(posAmpersand, posAmpersand + 1, "_$AMP$_");
      int start = posAmpersand + 1;
      posAmpersand = sb.indexOf("@", start);
    }

    // Now look for slashes
    int posSlash = inString.indexOf("/");
    if(posSlash >=0 && sb == null)
    {
      // Need to do some work, the predicate has an attribute char
      sb = new StringBuffer(inString);
    }

    // Locate and replace the slashes
    while(posSlash >=0)
    {
      // Replace the slash with "_$SL$_"
      sb.replace(posSlash, posSlash + 1, "_$SL$_");
      int start = posSlash + 1;
      posSlash = sb.indexOf("/", start);
    }    

    // Now look for doubledots
    int posDD = inString.indexOf("..");
    if(posDD >=0 && sb == null)
    {
      // Need to do some work, the predicate has an attribute char
      sb = new StringBuffer(inString);
    }

    // Locate and replace the doubledots
    while(posDD >=0)
    {
      // Replace the slash with "_$DD$_"
      sb.replace(posDD, posDD + 2, "_$DD$_");
      int start = posDD + 1;
      posDD = sb.indexOf("..", start);
    }        

    // Now look for singledots
    int posSD = -1;
    if(sb != null)
      posSD = sb.indexOf(".");
    else
    {
      posSD = inString.indexOf(".");

      if(posSD >=0)
      {
        // Need to do some work, the predicate has an attribute char
        sb = new StringBuffer(inString);
      }      
    }

    // Locate and replace the singledots
    while(posSD >=0)
    {
      // Move start position to beyond the current dot
      int start = posSD + 1;
      // Need to be a bit careful here, we don't want to replace
      // decimal points
      if( start < sb.length() && Character.isDigit(sb.charAt(posSD+1)))
      {
        // Subsequent char is a digit
        posSD = -1;
      }
      else
      {
        // Test previous char
        if(posSD > 0)
        {
          if( Character.isDigit(sb.charAt(posSD-1)))
          {
            // Prev char is a digit
            posSD = -1;
          }
        }
      }
      
      // Replace the dot with "_$SD$_"      
      if(posSD >= 0)
      {
        sb.replace(posSD, posSD + 1, "_$SD$_");
      }
 
      // Any more dots?
      posSD = sb.indexOf(".", start);
    }            
    
    // We've finished
    if (sb != null)
    {
      outString = sb.toString();
    }
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
      tc.exit(this,cclass, "replaceSpecialsWithSub", outString);     
    return outString; 
  }
860281
private static void do_replace(GlobData rdata, Context cx,
                                   RegExpImpl regExpImpl)
    {
        StringBuilder charBuf = rdata.charBuf;
        int cp = 0;
        String da = rdata.repstr;
        int dp = rdata.dollar;
        if (dp != -1) {
            int[] skip = new int[1];
            do {
                int len = dp - cp;
                charBuf.append(da.substring(cp, dp));
                cp = dp;
                SubString sub = interpretDollar(cx, regExpImpl, da,
                                                dp, skip);
                if (sub != null) {
                    len = sub.length;
                    if (len > 0) {
                        charBuf.append(sub.str, sub.index, sub.index + len);
                    }
                    cp += skip[0];
                    dp += skip[0];
                } else {
                    ++dp;
                }
                dp = da.indexOf('$', dp);
            } while (dp >= 0);
        }
        int daL = da.length();
        if (daL > cp) {
            charBuf.append(da.substring(cp, daL));
        }
    }
417511
public static String escapeHTML(String s) {
    // this version is much faster than using s.replaceAll()
    StringBuilder sb = new StringBuilder();
    int n = s.length();
    for (int i = 0; i < n; i++) {
      char c = s.charAt(i);
      switch (c) {
        case '<':
          sb.append("&lt;");
          break;
        case '>':
          sb.append("&gt;");
          break;
        case '&':
          sb.append("&amp;");
          break;
        case '"':
          sb.append("&quot;");
          break;
        default:
          sb.append(c);
          break;
      }
    }
    return sb.toString();
  }
1064058
public static void replaceAll(StringBuffer haystack, String needle, String newNeedle, int interval) {
		if (needle == null) {
			throw new IllegalArgumentException("string to replace can not be empty");
		}
		int idx = haystack.indexOf(needle);
		int nextIdx = -1;
		int processedChunkSize = idx;
		int needleLength = needle.length();
		int newNeedleLength = newNeedle.length();
		while (idx != -1/* && idx < haystack.length()*/) {
			if (processedChunkSize >= interval) {
				haystack.replace(idx, idx + needleLength, newNeedle);
				nextIdx = haystack.indexOf(needle, idx + newNeedleLength);
				processedChunkSize = nextIdx - idx;//length of replacement is not included
				idx = nextIdx;
			}
			else {
				nextIdx = haystack.indexOf(needle, idx + newNeedleLength);
				processedChunkSize += nextIdx - idx;
				idx = nextIdx;
				if (newNeedleLength == 0) {
					return;
				}
			}
		}
	}
315273
private Optional<MetricValue> rewrite_(MetricValue v) {
        return v.asString()
                .map(pattern_::matcher)
                .filter(matcher -> matcher.find())
                .map(this::resolve_template_args_)
                .map(template_::apply)
                .map(MetricValue::fromStrValue);
    }
1296178
private String replaceParameters(String text) {
    	Map<String, Object> params  = bean.getParametersBean().getParamValues();
        for  (String param : params.keySet()) {
             text = StringUtil.replace(text, "\\$P\\{" + param + "\\}", StringUtil.getValueAsString(params.get(param), null));
        }
        return text;
    }
1253986
function updateQueryStringParameter(uri, key, value) {
            var re = new RegExp("([?|&])" + key + "=.*?(&|#|$)", "i");

            if (uri.match(re)) {
                return uri.replace(re, '$1' + key + "=" + value + '$2');
            } else {
                var hash = '';

                if (uri.indexOf('#') !== -1) {
                    hash = uri.replace(/.*#/, '#');
                    uri = uri.replace(/#.*/, '');
                }

                var separator = uri.indexOf('?') !== -1 ? "&" : "?";
                return uri + separator + key + "=" + value + hash;
            }
        }
895131
function replaceAll(str, find, replace) {
	let s = '',
		index,
		next;
	while (~(next = str.indexOf(find, index))) {
		s += str.substring(index, next) + replace;
		index = next + find.length;
	}
	return s + str.substring(index);
}
165392
public static String replaceFullRequestContent(
            String requestContentTemplate, String replacementString) {
        return (requestContentTemplate.replace(
                PcConstants.COMMAND_VAR_DEFAULT_REQUEST_CONTENT,
                replacementString));
    }
1006158
public static String replaceServerUri(String link, String server) {

        String serverUriStr = server;

        if (CmsStringUtil.isEmptyOrWhitespaceOnly(serverUriStr)) {
            return link;
        }
        try {
            URI serverUri = new URI(serverUriStr);
            URI linkUri = new URI(link);
            URI result = new URI(
                serverUri.getScheme(),
                serverUri.getAuthority(),
                linkUri.getPath(),
                linkUri.getQuery(),
                linkUri.getFragment());
            return result.toString();
        } catch (URISyntaxException e) {
            LOG.error(e.getLocalizedMessage(), e);
            return link;
        }

    }