Java Regex String Replace First replaceFirstIgnoreCase(String source, String search, String replace)

Here you can find the source of replaceFirstIgnoreCase(String source, String search, String replace)

Description

replace First Ignore Case

License

LGPL

Declaration

public static String replaceFirstIgnoreCase(String source, String search, String replace) 

Method Source Code

//package com.java2s;
/*//from w  w w  .j a v  a  2s. c o  m
 * MoXie (SysTem128@GMail.Com) 2009-3-11 10:06:36
 * 
 * Copyright © 2008-2009 Zoeey.Org
 * Code license: GNU Lesser General Public License Version 3
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 */

import java.util.regex.Pattern;

public class Main {

    public static String replaceFirstIgnoreCase(String source, String search, String replace) {
        source = (source == null) ? "" : source;
        if (search == null) {
            return source;
        }
        replace = replace == null ? "" : replace;
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append("((?i)");
        strBuilder.append(Pattern.quote(search));
        strBuilder.append(")");
        return Pattern.compile(strBuilder.toString()).matcher(source).replaceFirst(replace);
    }

    public static String replaceFirst(String source, String search, String replace) {
        source = (source == null) ? "" : source;
        if (search == null) {
            return source;
        }
        replace = replace == null ? "" : replace;
        return Pattern.compile(Pattern.quote(search)).matcher(source).replaceFirst(replace);
    }
}

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 content, String regex, String replacement)
  4. replaceFirst(String str, String regex, String replacement, int patternFlag)
  5. replaceFirst(StringBuffer buf, StringBuffer aux, Pattern pattern, String replacement, int pos)
  6. replaceFirstOccurrence(String originalText, String oldString, String newString)
  7. replaceFirstStatement(String source, String regex, String statement)
  8. replaceLeading(String string, char replaced, char replacedBy)