Here you can find the source of replace(String inString, String oldPattern, String newPattern)
public static String replace(String inString, String oldPattern, String newPattern)
//package com.java2s; //License from project: Open Source License import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static String replace(String regex, int group, String str, String replacement) { if (isNullOrEmpty(str)) { return null; }/*from w ww .ja v a 2 s . c om*/ Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); while (m.find()) { String temp = m.group(); String target = m.group(group); String temp1 = m.group().replace(target, replacement); str = str.replace(temp, temp1); } return str; } public static String replace(String inString, String oldPattern, String newPattern) { if (!hasLength(inString) || !hasLength(oldPattern) || newPattern == null) { return inString; } StringBuilder sb = new StringBuilder(); int pos = 0; // our position in the old string int index = inString.indexOf(oldPattern); // the index of an occurrence we've found, or -1 int patLen = oldPattern.length(); while (index >= 0) { sb.append(inString.substring(pos, index)); sb.append(newPattern); pos = index + patLen; index = inString.indexOf(oldPattern, pos); } sb.append(inString.substring(pos)); // remember to append any characters to the right of a match return sb.toString(); } public static boolean isNullOrEmpty(String str) { if (str == null || str.isEmpty()) { return true; } return false; } /** * Check that the given {@code CharSequence} is neither {@code null} nor * of length 0. * <p>Note: this method returns {@code true} for a {@code CharSequence} * that purely consists of whitespace. * <p><pre class="code"> * StringUtils.hasLength(null) = false * StringUtils.hasLength("") = false * StringUtils.hasLength(" ") = true * StringUtils.hasLength("Hello") = true * </pre> * @param str the {@code CharSequence} to check (may be {@code null}) * @return {@code true} if the {@code CharSequence} is not {@code null} and has length * @see #hasText(String) */ public static boolean hasLength(CharSequence str) { return (str != null && str.length() > 0); } /** * Check that the given {@code String} is neither {@code null} nor of length 0. * <p>Note: this method returns {@code true} for a {@code String} that * purely consists of whitespace. * @param str the {@code String} to check (may be {@code null}) * @return {@code true} if the {@code String} is not {@code null} and has length * @see #hasLength(CharSequence) * @see #hasText(String) */ public static boolean hasLength(String str) { return hasLength((CharSequence) str); } }