Java String Substritute substitute(String original, String match, String subst)

Here you can find the source of substitute(String original, String match, String subst)

Description

Find all occurences of the "match" in original, and substitute the "subst" string.

License

Open Source License

Parameter

Parameter Description
original starting string
match string to match
subst string to substitute

Return

a new string with substitutions

Declaration

static public String substitute(String original, String match, String subst) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w w  w  .  j ava 2s. co  m*/
     * Find all occurences of the "match" in original, and substitute the "subst" string.
     *
     * @param original starting string
     * @param match    string to match
     * @param subst    string to substitute
     * @return a new string with substitutions
     */
    static public String substitute(String original, String match, String subst) {
        String s = original;
        int pos;
        while (0 <= (pos = s.indexOf(match))) {
            StringBuilder sb = new StringBuilder(s);
            s = sb.replace(pos, pos + match.length(), subst).toString();
        }
        return s;
    }

    /**
     * Find all occurences of match strings in original, and substitute the corresponding
     * subst string.
     *
     * @param original starting string
     * @param match    array of strings to match
     * @param subst    array of strings to substitute
     * @return a new string with substitutions
     */
    static public String substitute(String original, String[] match, String[] subst) {

        boolean ok = true;
        for (String aMatch : match) {
            if (original.contains(aMatch)) {
                ok = false;
                break;
            }
        }
        if (ok) {
            return original;
        }

        // gotta do it;
        StringBuilder sb = new StringBuilder(original);
        for (int i = 0; i < match.length; i++) {
            substitute(sb, match[i], subst[i]);
        }
        return sb.toString();
    }

    /**
     * Find all occurences of the "match" in original, and substitute the "subst" string,
     * directly into the original.
     *
     * @param sbuff starting string buffer
     * @param match string to match
     * @param subst string to substitute
     */
    static public void substitute(StringBuilder sbuff, String match, String subst) {
        int pos, fromIndex = 0;
        int substLen = subst.length();
        int matchLen = match.length();
        while (0 <= (pos = sbuff.indexOf(match, fromIndex))) {
            sbuff.replace(pos, pos + matchLen, subst);
            fromIndex = pos + substLen; // make sure dont get into an infinite loop
        }
    }

    /**
     * Replace any char "out" in s with "in".
     *
     * @param s   string to replace
     * @param out replace this character
     * @param in  with this string
     * @return modified string if needed
     */
    static public String replace(String s, char out, String in) {
        if (s.indexOf(out) < 0) {
            return s;
        }

        // gotta do it
        StringBuilder sb = new StringBuilder(s);
        replace(sb, out, in);
        return sb.toString();
    }

    /**
     * Replace all occurrences of any char in replaceChar with corresponding String in replaceWith
     *
     * @param x           operate on this string
     * @param replaceChar get rid of these
     * @param replaceWith replace with these
     * @return resulting string
     */
    static public String replace(String x, char[] replaceChar, String[] replaceWith) {
        // common case no replacement
        boolean ok = true;
        for (char aReplaceChar : replaceChar) {
            int pos = x.indexOf(aReplaceChar);
            ok = (pos < 0);
            if (!ok)
                break;
        }
        if (ok)
            return x;

        // gotta do it
        StringBuilder sb = new StringBuilder(x);
        for (int i = 0; i < replaceChar.length; i++) {
            int pos = x.indexOf(replaceChar[i]);
            if (pos >= 0) {
                replace(sb, replaceChar[i], replaceWith[i]);
            }
        }

        return sb.toString();
    }

    /**
     * Replaces all occurrences of "pattern" in "string" with "value"
     *
     * @param string  string to munge
     * @param pattern pattern to replace
     * @param value   replacement value
     * @return munged string
     */
    public static String replace(String string, String pattern, String value) {
        if (pattern.length() == 0)
            return string;

        if (!string.contains(pattern))
            return string;

        // ok gotta do it
        StringBuilder returnValue = new StringBuilder();
        int patternLength = pattern.length();
        while (true) {
            int idx = string.indexOf(pattern);
            if (idx < 0)
                break;

            returnValue.append(string.substring(0, idx));
            if (value != null)
                returnValue.append(value);

            string = string.substring(idx + patternLength);
        }
        returnValue.append(string);
        return returnValue.toString();
    }

    /**
     * Replace any char "out" in sb with String "in".
     *
     * @param sb  StringBuilder to replace
     * @param out repalce this character
     * @param in  with this string
     */
    static public void replace(StringBuilder sb, char out, String in) {
        for (int i = 0; i < sb.length(); i++) {
            if (sb.charAt(i) == out) {
                sb.replace(i, i + 1, in);
                i += in.length() - 1;
            }
        }
    }

    /**
     * Replace any of the characters from out with corresponding character from in
     *
     * @param sb  the StringBuilder
     * @param out get rid of any of these characters
     * @param in  replacing with the character at same index
     */
    static public void replace(StringBuilder sb, String out, String in) {
        for (int i = 0; i < sb.length(); i++) {
            int c = sb.charAt(i);
            for (int j = 0; j < out.length(); j++) {
                if (out.charAt(j) == c)
                    sb.setCharAt(i, in.charAt(j));
            }
        }
    }
}

Related

  1. substitute(final String input, final String pattern, final String sub)
  2. substitute(String in, String find, String newString)
  3. substitute(String s, String from, String to)
  4. substitute(String str, CharSequence... substitutionSeqs)
  5. substitute(String str, String from, String to)
  6. substitute(String str, String source, String target)