Java Regex String Replace replace(String text, String find, String match, boolean useRegex, boolean isCaseSensitive)

Here you can find the source of replace(String text, String find, String match, boolean useRegex, boolean isCaseSensitive)

Description

replace

License

Mozilla Public License

Declaration

public static String replace(String text, String find, String match, boolean useRegex,
            boolean isCaseSensitive) 

Method Source Code

//package com.java2s;
/** This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *//*from w  ww.j  av  a  2 s .  c o  m*/

import java.util.regex.*;

public class Main {
    public static String replace(String text, String find, String match, boolean useRegex,
            boolean isCaseSensitive) {
        if (useRegex) {
            if (isCaseSensitive) {
                return text.replaceAll(find, match);
            } else {
                return text.replaceAll("(?i)" + find, match);
            }
        } else {
            if (isCaseSensitive) {
                return text.replace(find, match);
            } else {
                return text.replaceAll("(?i)" + Matcher.quoteReplacement(find), match);
            }
        }
    }
}

Related

  1. replace(String string, Pattern pattern, String replacement)
  2. replace(String string, Pattern[] patterns, String[] replacements)
  3. replace(String string, Properties table)
  4. replace(String string, String pattern, String replacement, boolean literal)
  5. replace(String text, Map vars, String prefix)
  6. replace(String text, String findPattern, String replacePattern)
  7. replace(String text, String targetText, String newText)
  8. replace(String text, String[] searchStrings, String[] replacements)
  9. replace(String val)