Java Regex String Replace All replaceAllIgnoreCase(String source, String oldstring, String newstring)

Here you can find the source of replaceAllIgnoreCase(String source, String oldstring, String newstring)

Description

replace All Ignore Case

License

Apache License

Declaration

public static String replaceAllIgnoreCase(String source, String oldstring, String newstring) 

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 replaceAllIgnoreCase(String source, String oldstring, String newstring) {
        Pattern p = Pattern.compile(oldstring, 34);
        Matcher m = p.matcher(source);
        return m.replaceAll(newstring);
    }/* w w w  .j  a  v a2 s. c  o  m*/

    public static String replaceAll(String content, String targetWord, String replaceWord) {
        int lastIndex;
        if (content == null)
            return null;
        StringBuffer text = new StringBuffer();
        int contentLength = content.length();
        int targetWordLenght = targetWord.length();
        int startIndex = 0;

        while ((lastIndex = content.indexOf(targetWord, startIndex)) >= 0) {
            text.append(content.substring(startIndex, lastIndex));
            text.append(replaceWord);
            startIndex = lastIndex + targetWordLenght;
        }
        if (startIndex < contentLength) {
            text.append(content.substring(startIndex));
        }
        return text.toString();
    }

    public static int indexOf(String str, char strChar) {
        if (isEmpty(str)) {
            return -1;
        }
        return str.indexOf(strChar);
    }

    /**
     * <pre>
     * ExmayStringUtils.indexOf(null, *)          = -1
     * ExmayStringUtils.indexOf(*, null)          = -1
     * ExmayStringUtils.indexOf("", "")           = 0
     * ExmayStringUtils.indexOf("aabaabaa", "a")  = 0
     * ExmayStringUtils.indexOf("aabaabaa", "b")  = 2
     * ExmayStringUtils.indexOf("aabaabaa", "ab") = 1
     * ExmayStringUtils.indexOf("aabaabaa", "")   = 0
     * </pre>
     * 
     * @param str
     * @param searchStr
     * @return
     */
    public static int indexOf(String str, String searchStr) {
        if (str == null || searchStr == null) {
            return -1;
        }
        return str.indexOf(searchStr);
    }

    public static int indexOf(String str, char searchChar, int startPos) {
        if (isEmpty(str)) {
            return -1;
        }
        return str.indexOf(searchChar, startPos);
    }

    public static String subString(String src, int length) {
        if (src == null) {
            return null;
        }
        int i = src.length();
        if (i > length) {
            return src.substring(0, length);
        }
        return src;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0 || str.equals("") || str.matches("\\s*");
    }
}

Related

  1. replaceAll(String string, Pattern[] patterns, String replacement)
  2. replaceAll(String target, String from, String to)
  3. replaceAll(String text, Pattern pattern, String replace)
  4. replaceAll(StringBuilder sb, String regex, String replacement)
  5. replaceAllBackreference(String text, String regex, String replacement)
  6. replaceAllLiteral(@Nonnull String value, @Nonnull Pattern pattern, @Nonnull String replace)
  7. replaceAllPerLine(Pattern pattern, String value, String replacement)
  8. replaceAllStrictly(String src, String search, String replacement, boolean entirelyMatch, boolean caseSensitive)
  9. replaceAllTotal(String what, Pattern p, String replacement)