Java Regex String Replace First replaceFirst(String content, String regex, String replacement)

Here you can find the source of replaceFirst(String content, String regex, String replacement)

Description

replace First

License

Apache License

Declaration

public static String replaceFirst(String content, String regex, String replacement) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static String replaceFirst(String content, String regex, String replacement) {
        if (isNone(content)) {
            return content;
        }/*from   w w w . j av a2s .  co  m*/
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            content = matcher.replaceFirst(replacement);
        }
        return content;
    }

    public static boolean isNone(String str) {
        if (str == null || "".equals(str)) {
            return true;
        } else {
            return false;
        }
    }
}

Related

  1. replaceFirst(final String text, final Pattern regex, final String replacement)
  2. replaceFirst(final String text, final String regex, final String replacement)
  3. replaceFirst(String str, String regex, String replacement, int patternFlag)
  4. replaceFirst(StringBuffer buf, StringBuffer aux, Pattern pattern, String replacement, int pos)
  5. replaceFirstIgnoreCase(String source, String search, String replace)
  6. replaceFirstOccurrence(String originalText, String oldString, String newString)