Java String Substitute 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;

import java.util.*;

public class Main {
    /**//  ww w  . j ava2 s .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))) {
            StringBuffer sb = new StringBuffer(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 (int i = 0; i < match.length; i++) {
            if (0 <= original.indexOf(match[i])) {
                ok = false;
                break;
            }
        }
        if (ok) {
            return original;
        }

        // gotta do it;
        StringBuffer sb = new StringBuffer(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(StringBuffer 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
        }
    }

    /**
     * Convert the list of objects to a list of strings.
     *
     * @param l List of objects
     * @return List of strings.
     */
    public static List toString(List l) {
        List stringList = new ArrayList();
        for (int i = 0; i < l.size(); i++) {
            stringList.add(l.get(i).toString());
        }
        return stringList;
    }

    /**
     * Create a string representation of the given array
     *
     * @param array array to print
     * @return array as a String
     */
    public static String toString(Object[] array) {
        StringBuffer buf = new StringBuffer();
        buf.append(": ");
        for (int i = 0; i < array.length; i++) {
            buf.append("[");
            buf.append(i);
            buf.append("]: ");
            buf.append((array[i] == null) ? "null" : array[i]);
            buf.append(" ");
        }
        return buf.toString();
    }

    /**
     * Replace all occurences of replaceChar with 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 (int i = 0; i < replaceChar.length; i++) {
            int pos = x.indexOf(replaceChar[i]);
            ok = (pos < 0);
            if (!ok)
                break;
        }
        if (ok)
            return x;

        // gotta do it
        StringBuffer sb = new StringBuffer(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();
    }

    /**
     * Replace any char "out" in sb with "in".
     *
     * @param sb  StringBuffer to replace
     * @param out repalce this character
     * @param in  with this string
     */
    static public void replace(StringBuffer 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 char "out" in s with "in".
     *
     * @param s   string to replace
     * @param out repalce 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
        StringBuffer sb = new StringBuffer(s);
        replace(sb, out, in);
        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;
        }
        StringBuffer returnValue = new StringBuffer();
        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();
    }

    /**
     * A utility method to an append to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null the we append to the StringBuffer the results of s1.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 object to append
     * @return StringBuffer with appended object
     */
    public static StringBuffer append(StringBuffer sb, Object s1) {
        if (sb == null) {
            sb = new StringBuffer();
        }
        sb.append((s1 == null) ? "null" : s1.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2) {
        sb = append(sb, s1);
        sb.append((s2 == null) ? "null" : s2.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2, Object s3) {
        sb = append(sb, s1, s2);
        sb.append((s3 == null) ? "null" : s3.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @param s4 fourth object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2, Object s3, Object s4) {
        sb = append(sb, s1, s2, s3);
        sb.append((s4 == null) ? "null" : s4.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @param s4 fourth object to append
     * @param s5 fifth object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2, Object s3, Object s4, Object s5) {
        sb = append(sb, s1, s2, s3, s4);
        sb.append((s5 == null) ? "null" : s5.toString());
        return sb;
    }
}

Related

  1. stringSubstitution(String argStr, Hashtable vars)
  2. substitute(String str, Hashtable vars, String delimiter)
  3. substituteProperty(String value, Properties coreProperties)