Java String Replace replace(String string, String pattern, String value)

Here you can find the source of replace(String string, String pattern, String value)

Description

Replaces all occurrences of "pattern" in "string" with "value"

License

Open Source License

Parameter

Parameter Description
string string to munge
pattern pattern to replace
value replacement value

Return

munged string

Declaration

public static String replace(String string, String pattern, String value) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**//from   w w w. j  av  a2s .c  o  m
     * 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();
    }

    /**
     * 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();
    }

    /**
     * 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. replace(String source, Properties properties)
  2. replace(String source, String oldChars, String newChars)
  3. replace(String source, String stringToReplace, String replacementString, boolean matchCase)
  4. replace(String str)
  5. replace(String string, String pattern, String value)
  6. replace(String target, String replacement, String... args)
  7. replaceAll(String source, String toReplace, String replacement)
  8. replaceEmitKeys(String map, Collection newKeys)
  9. replaceFirst(String input, String search, String replacement)